Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://perldoc.perl.org/perlthrtut.html" rel="nofollow">Perl threads</a> will take advantage of multiple cores and processors. The main pro of threads is its fairly easy to share data between the threads and coordinate their activities. A forked process cannot easily return data to the parent nor coordinate amongst themselves.</p> <p>The main cons of Perl threads is they are relatively expensive to create compared to a fork, they must copy the entire program and all its data; you must have them compiled into your Perl; and they can be buggy, the older the Perl, the buggier the threads. If your work is expensive, the creation time should not matter.</p> <p>Here's an example of how you might do it with threads. There's many ways to do it, this one uses <a href="https://metacpan.org/module/Thread%3a%3aQueue" rel="nofollow">Thread::Queue</a> to create a big list of work your worker threads can share. When the queue is empty, the threads exit. The main advantages are that its easier to control how many threads are active, and you don't have to create a new, expensive thread for each bit of work.</p> <p>This example shoves all the work into the queue at once, but there's no reason you can't add to the queue as you go. If you were to do that, you'd use <code>dequeue</code> instead of <code>dequeue_nb</code> which will wait around for more input.</p> <pre><code>use strict; use warnings; use threads; use Thread::Queue; # Dummy work routine sub start_XFOIL_instance { my $arg = shift; print "$arg\n"; sleep 1; } # Read in dummy data my @xfoil_args = &lt;DATA&gt;; chomp @xfoil_args; # Create a queue to push work onto and the threads to pull work from # Populate it with all the data up front so threads can finish when # the queue is exhausted. Makes things simpler. # See https://rt.cpan.org/Ticket/Display.html?id=79733 my $queue = Thread::Queue-&gt;new(@xfoil_args); # Create a bunch of threads to do the work my @threads; for(1..4) { push @threads, threads-&gt;create( sub { # Pull work from the queue, don't wait if its empty while( my $xfoil_args = $queue-&gt;dequeue_nb ) { # Do the work start_XFOIL_instance($xfoil_args); } # Yell when the thread is done print "Queue empty\n"; }); } # Wait for threads to finish $_-&gt;join for @threads; __DATA__ blah foo bar baz biff whatever up down left right </code></pre>
 

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