Note that there are some explanatory texts on larger screens.

plurals
  1. POUse cases for ithreads (interpreter threads) in Perl and rationale for using or not using them?
    primarykey
    data
    text
    <p>If you want to learn <em>how</em> to use Perl interpreter threads, there's good documentation in <a href="http://perldoc.perl.org/perlthrtut.html" rel="nofollow noreferrer"><code>perlthrtut</code> (threads tutorial)</a> and <a href="https://metacpan.org/module/threads" rel="nofollow noreferrer">the <code>threads</code> pragma manpage</a>. It's definitely good enough to write some simple scripts.</p> <p>However, I have found little guidance on the web on <em>why</em> and <em>what</em> to sensibly use Perl's interpreter threads for. In fact, there's not much talk about them, and if people talk about them it's quite often to discourage people from using them.</p> <p>These threads, available when <code>perl -V:useithreads</code> is <code>useithreads='define';</code> and unleashed by <code>use threads</code>, are also called <em>ithreads</em>, and maybe more appropriately so as they are very different from threads as offered by the Linux or Windows operating systems or the Java VM in that nothing is shared by default and instead a lot of data is copied, not just the thread stack, thus significantly increasing the process size. (To see the effect, load some modules in a test script, then create threads in a loop pausing for key presses each time around, and watch memory rise in task manager or <code>top</code>.)</p> <blockquote> <p>[...] every time you start a thread all data structures are copied to the new thread. And when I say all, I mean all. This e.g. includes package stashes, global variables, lexicals in scope. Everything!</p> </blockquote> <p>-- <a href="http://www.perlmonks.org/?node_id=288022" rel="nofollow noreferrer">Things you need to know before programming Perl ithreads (Perlmonks 2003)</a></p> <p>When researching the subject of Perl ithreads, you'll see people discouraging you from using them (<a href="https://stackoverflow.com/a/370670/269126">"extremely bad idea", "fundamentally flawed"</a>, or <a href="https://stackoverflow.com/a/1480673/269126">"never use ithreads for anything"</a>).</p> <p><a href="http://perldoc.perl.org/perlthrtut.html#What-kind-of-threads-are-Perl-threads?" rel="nofollow noreferrer">The Perl thread tutorial highlights that "Perl Threads Are Different"</a>, but it doesn't much bother to explain how they are different and what that means for the user.</p> <p>A useful but very brief explanation of what ithreads really are is <a href="https://metacpan.org/module/Coro#WINDOWS-PROCESS-EMULATION" rel="nofollow noreferrer">from the <code>Coro</code> manpage under the heading <em>WINDOWS PROCESS EMULATION</em></a>. The author of that module (<em>Coro - the only real threads in perl</em>) also discourages using Perl interpreter threads.</p> <p>Somewhere I read that compiling perl with threads enabled will result in a significantly slower interpreter.</p> <p>There's a Perlmonks page from 2003 (<a href="http://www.perlmonks.org/?node_id=288022" rel="nofollow noreferrer">Things you need to know before programming Perl ithreads</a>), in which the author asks: "Now you may wonder why Perl ithreads didn't use fork()? Wouldn't that have made a lot more sense?" This seems to have been written by the author of the <code>forks</code> pragma. Not sure the info given on that page still holds true in 2012 for newer Perls.</p> <p>Here are some guidelines for usage of threads in Perl I have distilled from my readings (maybe erroneously so):</p> <ul> <li>Consider using non-blocking IO instead of threads, like with <a href="https://metacpan.org/module/HTTP::Async" rel="nofollow noreferrer"><code>HTTP::Async</code></a>, or <a href="https://stackoverflow.com/a/7850656/269126"><code>AnyEvent::Socket</code>, or <code>Coro::Socket</code></a>.</li> <li>Consider using Perl interpreter threads <a href="https://metacpan.org/module/Coro#WINDOWS-PROCESS-EMULATION" rel="nofollow noreferrer">on Windows only, not on UNIX because on UNIX, forks are more efficient both for memory and execution speed</a>.</li> <li>Create threads at beginning of program, not when memory concumption already considerable - see <a href="http://perldoc.perl.org/perlthrtut.html#Performance-considerations" rel="nofollow noreferrer">"ideal way to reduce these costs" in <code>perlthrtut</code></a>.</li> <li>Minimize communication between threads <a href="https://stackoverflow.com/q/1691836/269126">because it's slow</a> (all answers on that page).</li> </ul> <p>So far my research. Now, thanks for any more light you can shed on this issue of threads in Perl. What are some sensible use cases for ithreads in Perl? What is the rationale for using or not using them?</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.
 

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