Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Thread parallel execution (join blocks)

by Anonymous Monk
on May 08, 2013 at 02:17 UTC ( #1032560=note: print w/ replies, xml ) Need Help??


in reply to Thread parallel execution

Did you read about what join does? It blocks. Try

use threads; my $thr = threads->create ( sub { for (5..10) { print $_,"\n"; sleep(1);}}); my $thr2 = threads->create ( sub { for (10..19) { print $_,"\n";}}); $thr2->join(); $thr->join();


Comment on Re: Thread parallel execution (join blocks)
Download Code
Re^2: Thread parallel execution (joinable)
by Anonymous Monk on May 08, 2013 at 02:26 UTC
    #!/usr/boin/perl -- use strict; use warnings; use threads; for my $ix ( 0 .. 20 ) { threads->create( sub { sleep rand $ix; print join ' ', $$, ix => $ix, tid => threads->tid(), "\n" +; } ); } exit if @ARGV; while( threads->list ) { for my $joinable ( threads->list( threads::joinable ) ) { $joinable->join; } } __END__ $ perl fudge exit 1044 ix 0 tid 1 1044 ix 1 tid 2 1044 ix 2 tid 3 1044 ix 4 tid 5 1044 ix 17 tid 18 Perl exited with active threads: 16 running and unjoined 5 finished and unjoined 0 running and detached $ perl fudge 1948 ix 0 tid 1 1948 ix 1 tid 2 1948 ix 2 tid 3 1948 ix 3 tid 4 1948 ix 4 tid 5 1948 ix 7 tid 8 1948 ix 17 tid 18 1948 ix 9 tid 10 1948 ix 12 tid 13 1948 ix 13 tid 14 1948 ix 18 tid 19 1948 ix 6 tid 7 1948 ix 5 tid 6 1948 ix 10 tid 11 1948 ix 8 tid 9 1948 ix 14 tid 15 1948 ix 19 tid 20 1948 ix 11 tid 12 1948 ix 15 tid 16 1948 ix 16 tid 17 1948 ix 20 tid 21
Re^2: Thread parallel execution (join blocks)
by arunbhargav (Novice) on May 08, 2013 at 03:03 UTC
    Thanks, My interpretation of join was it would only attach this thread to the main thread, not block until it finishes executing. Well, this clears up a lot.
      My interpretation of join was it would only attach this thread to the main thread, not block until it finishes executing.

      I'm not getting on your case here, but this is a common enough misconception that it is worth drawing attention to.

      How could it "attach this thread to the main thread" without "block until it finishes executing"?

      What would "attachment" without "waiting" even mean?

      "join" is an awful method name. The primary purpose of join is to retrieve the results of the subroutine that was run in the thread.

      If the thread has finished before the call to join is made, then it can return immediately. However, if the thread hasn't finished, then join has no option but to wait until it does.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (19)
As of 2014-04-16 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (427 votes), past polls