My question is (Once i remove the sleeps) does the below method make 10 calls concurrently at the same time and then return the data for all 10 children. Or will this method simply make 10 calls one at a time defeating the purpose handily.

I'm not sure why you've posted the fork code if your question is about threads? But....

Your threaded code can be reduced (for brevity) to:

#! perl -slw use strict; use threads; my @threads = map async( sub{ my $num = shift; print "started thread $num"; sleep $num; print "done with thread $num"; return $num; }, $_ ), 1 .. 3; print "Done with ", $_->join for @threads; __END__ C:\test>junk52 started thread 1 started thread 2 started thread 3 done with thread 1 Done with 1 done with thread 2 Done with 2 done with thread 3 Done with 3

Now my guess is that your real question is: why do all three threads finish in the same order as I started them.

The answer is: if you start thread 1 and ask it to sleep for 1 second; and start thread 2 and ask it to sleep for 2 seconds; and thread 3 for 3 seconds; and so on. Then the threads will inevitably finish in the same order you started them in.

If you want to reassure yourself that threads actually do run concurrently, you should ask them to sleep for random numbers of seconds:

#! perl -slw use strict; use threads; my @threads = map async( sub{ my( $num, $sleep ) = @_; print "started thread $num to sleep for $sleep seconds"; sleep $sleep; print "done with thread $num"; return $num; }, $_, 2+int( rand 3 ) ), 1 .. 3; print "Done with ", $_->join for @threads; __END__ C:\test>junk52 started thread 1 to sleep for 4 seconds started thread 2 to sleep for 3 seconds started thread 3 to sleep for 2 seconds done with thread 3 done with thread 2 done with thread 1 Done with 1 Done with 2 Done with 3

Now you can see (from the ordering of the "done with ..." messages), that the order they complete in depends upon how long they run. And if you time them, you'll see that the overall time is the largest $sleep value, not the sum of all the $sleep values, as it would be if they ran serially.

Finally, you're probably going to ask why, if thread 3 finished first, why did you get it results last?

The answer is, because that's what was asked for.

Just as in your original code when you pushed the thread handles in the order 1 .. 10 and then asked for the results (joined them) by iterating over that array in the same order.

When the above code does:

print "Done with ", $_->join for @threads;

It is asking for the results of threads in the same order the threads were started, regardless of the order in which they produced those results.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re: Perl Threads Question. by BrowserUk
in thread Perl Threads Question. by Monkomatic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.