Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

is_joinable question

by anaconda_wly (Scribe)
on Jun 04, 2013 at 03:47 UTC ( #1036876=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

Why main didn't wait until testfun finished in my example below? It seems is_joinable returned false. I found "a call to is_joinable returns true only if a thread has finished running, hasn't been detached and hasn't been joined already". How to judge a running but not join thread?

Hi, Anonymous Monk, I changed my code as below. If I have requirement to judge before join...how to write the condition...

#/usr/bin/perl -w use strict; use warnings; use threads; my @arrayRunningThreads = (); &main; sub main { my $thrHandle = threads->create(\&detachedThreads); $thrHandle->detach(); push @arrayRunningThreads, $thrHandle; for(1...3){ $thrHandle = threads->create(\&MustJoin); push @arrayRunningThreads, $thrHandle; } foreach (@arrayRunningThreads){ $_->join() if( $_->is_joinable() ); } print "End. \n"; } sub detachedThreads { sleep 3; print "exiting thread.\n"; } sub MustJoin { sleep 3; print "exiting thread.\n"; }
output: End. Perl exited with active threads: 3 running and unjoined 0 finished and unjoined 1 running and detached

Comment on is_joinable question
Select or Download Code
Replies are listed 'Best First'.
Re: is_joinable question
by Anonymous Monk on Jun 04, 2013 at 04:02 UTC

    main did not wait because is_joinable returned false, so you didn't join

    if you want to wait for $thrHandle, simply join, join blocks, join waits until the thread over, until it is_joinable

    Study this example Re^2: Thread parallel execution (joinable)

      example you give works, but I didn't understand difference between threads->list( threads::joinable ) and is_joinable yet

      while( threads->list ) { for my $joinable ( threads->list( threads::joinable ) ) { $joinable->join; } }

        I can ask my kids "is mom home yet?" and get a yes or no answer. I can also ask them to wait outside until their mom gets back. The first is like is_joinable, the second is like join. One gets a yes or no answer, the other blocks until it is done.

        Update: Ignore me. Didn't read the question completely.

        --MidLifeXis

        example you give works, but I didn't understand difference between threads->list( threads::joinable ) and is_joinable yet

        Ok, and then what happened? Do you have a question? What do you understand?

        Do you understand the difference between a while loop and an if statement?

Re: is_joinable question
by Anonymous Monk on Jun 05, 2013 at 06:50 UTC

    The correct way to write that loop

    while( @arrayRunningThreads ){ my @keepers; for my $thread ( @arrayRunningThreads ){ if( $thread->is_joinable() ){ $thread ->join; } elsif( not $thread ->is_detached ){ push @keepers, $thread; } } @arrayRunningThreads = @keepers; }

    If you don't add detached threads you can write it as

    while( @arrayRunningThreads ){ my @keepers; for my $thread ( @arrayRunningThreads ){ if( $thread->is_joinable() ){ $thread ->join; } else { push @keepers, $thread; } } @arrayRunningThreads = @keepers; }

    Or the functional equivalent of what the above code does, relying on the fact that join blocks until a thread is_joinable

    for my $thread ( @arrayRunningThreads ){ $thread->join; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2015-07-31 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls