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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: is_joinable question
by Anonymous Monk on Jun 04, 2013 at 04:02 UTC | |
by anaconda_wly (Scribe) on Jun 04, 2013 at 06:03 UTC | |
by MidLifeXis (Monsignor) on Jun 04, 2013 at 13:06 UTC | |
by Anonymous Monk on Jun 05, 2013 at 06:03 UTC | |
|
Re: is_joinable question
by Anonymous Monk on Jun 05, 2013 at 06:50 UTC |