in reply to What is the correct way to finish multithreaded program?

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.

Replies are listed 'Best First'.
Re^2: What is the correct way to finish multithreaded program?
by Gangabass (Vicar) on May 05, 2014 at 11:18 UTC
    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 :-(
        I'm pushing undef into the queue with
        foreach ( 1 .. $config->{number_of_threads} ) { $q->enqueue(undef); }
        and I think this will cause thread to return:
        while ( my $org = $q->dequeue() ) { parse_org( $org, $mech ); $pq->enqueue($org); } say "Finishing worker [$thread_id]"; return;
        Also I have Finishing worker [SOME_ID] in the output...

      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