Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short answer is that they're quite heavy (you can't launch 100+ of them cheaply), and they exhibit unexpected behaviours (somewhat mitigated by recent CPAN modules).</p> <p><b>You <i>can</i> safely use Perl ithreads by treating them as independent Actors</b>.</p> <ol> <li>Create a Thread::Queue::Any for "work".</li> <li>Launch multiple ithreads and "result" Queues passing them the ("work" + own "result") Queues by closure.</li> <li>Load (require) all the remaining code your application requires (not before threads!)</li> <li>Add work for the threads into the Queue as required.</li> </ol> <p>In "worker" ithreads:</p> <ol> <li>Bring in any common code (for any kind of job)</li> <li>Blocking-dequeue a piece of work from the Queue</li> <li>Demand-load any other dependencies required for this piece of work.</li> <li>Do the work.</li> <li>Pass the result back to the main thread via the "result" queue.</li> <li>Back to 2.</li> </ol> <p>If some "worker" threads start to get a little beefy, and you need to limit "worker" threads to some number then launch new ones in their place, then create a "launcher" thread first, whose job it is to launch "worker" threads and hook them up to the main thread.</p> <p><b>What are the main problems with Perl ithreads?</b></p> <p>They're a little inconvenient with for "shared" data as you need to explicity do the sharing (not a big issue).</p> <p>You need to look out for the behaviour of objects with DESTROY methods as they go out of scope in some thread (if they're still required in another!)</p> <p><b>The big one</b>: Data/variables that aren't explicitly shared are <b>CLONED</b> into new threads. This is a performance hit and probably not at all what you intended. The work around is to launch ithreads from a pretty much "pristine" condition (not many modules loaded).</p> <p>IIRC, there are modules in the Threads:: namespace that help with making dependencies explicit and/or cleaning up cloned data for new threads.</p> <p>Also, IIRC, there's a slightly different model using ithreads called "Apartment" threads, implemented by Thread::Appartment which has a different usage pattern and another set of trade-offs. </p> <p><b>The upshot:</b></p> <p>Don't use them unless you know what you're doing :-)</p> <p>Fork <i>may</i> be more efficient on Unix, but the IPC story is <i>much</i> simpler for ithreads. (This may have been mitigated by CPAN modules since I last looked :-)</p> <p>They're <i>still</i> better than Python's threads.</p> <p>There might, one day, be something much better in Perl 6.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload