Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Thread parallel execution

by arunbhargav (Novice)
on May 08, 2013 at 02:06 UTC ( #1032557=perlquestion: print w/ replies, xml ) Need Help??
arunbhargav has asked for the wisdom of the Perl Monks concerning the following question:

use threads; my $thr = threads->create ( sub { for (5..10) { print $_,"\n"; sleep(10);}}); $thr->join(); my $thr2 = threads->create ( sub { for (10..19) { print $_,"\n";}}); $thr2->join();
I see that the above code produces the following o/p which has nuthing to do with parallel run, what am i missing here?
5 6 7 8 9 10 10 11 12 13 14 15 16 17 18 19
i was anticipating garbled output , not a sequential one

Comment on Thread parallel execution
Select or Download Code
Re: Thread parallel execution (join blocks)
by Anonymous Monk on May 08, 2013 at 02:17 UTC

    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();
      #!/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
      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: perlquestion [id://1032557]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-09-19 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls