Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

What is the correct way to finish multithreaded program?

by Gangabass (Priest)
on May 05, 2014 at 07:09 UTC ( #1085004=perlquestion: print w/ replies, xml ) Need Help??
Gangabass has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Monks

I have some multithreaded code which gives me warnings at the end:
Perl exited with active threads: 1 running and unjoined 2 finished and unjoined 0 running and detached
my $q = Thread::Queue->new(); my $pq = Thread::Queue->new(); my @threads = map { threads->create( \&worker, $_ ) } ( 1 .. $config->{number_of_t +hreads} ); push @threads, threads->create( \&controller ); my $mech = WWW::Mechanize->new(); $mech->agent_alias("Windows IE 6"); $mech->get( $config->{start_url} ); my @states = find_states($mech); foreach my $state (@states) { process_state($state); } while ( $q->pending() ) { sleep 1; } foreach ( 1 .. $config->{number_of_threads} ) { $q->enqueue(undef); } while ( $pq->pending() ) { sleep 1; } $pq->enqueue(undef); foreach my $thr (@threads) { if ( $thr->is_joinable() ) { $thr->join(); } }
sub process_state() is populating $q queue. Each thread contain something like this:
while ( my $org = $q->dequeue() ) { parse_org( $org, $mech ); $pq->enqueue($org); }
Thanks. Roman

Comment on What is the correct way to finish multithreaded program?
Select or Download Code
Re: What is the correct way to finish multithreaded program?
by Anonymous Monk on May 05, 2014 at 08:05 UTC

    Perl exited with active threads: 1 running and unjoined 2 finished and unjoined 0 running and detached

    Can you describe what this means?

Re: What is the correct way to finish multithreaded program?
by moritz (Cardinal) on May 05, 2014 at 08:46 UTC

    While I'm not an expert on threads, I believe this is the problem:

    foreach my $thr (@threads) { if ( $thr->is_joinable() ) { $thr->join(); } }

    You only join the threads that have already finished; instead you should join them all, i.e. wait for them to finish.

      Hmm... According to docs:
      $thr->is_joinable() Returns true if the thread has finished running, is not detached and has not yet been joined. In other words, the thread is ready to be joined, and a call to $thr->join() will not block.
      Previous block of code (while (..) {..}) used to wait threads to finish (I think). But may be I'm wrong :-(

        Previous block of code (while (..) {..}) used to wait threads to finish (I think). But may be I'm wrong :-(

        Maybe, it depends on more code than shown I think :)

        safest way to wait for threads to finish is to actually wait for them to finish :) blockingly  $_->join for threads->list

Log In?
Username:
Password:

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

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

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











    Results (178 votes), past polls