Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: What is the correct way to finish multithreaded program?

by Gangabass (Priest)
on May 05, 2014 at 11:18 UTC ( #1085040=note: print w/ replies, xml ) Need Help??


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

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 :-(


Comment on Re^2: What is the correct way to finish multithreaded program?
Download Code
Re^3: What is the correct way to finish multithreaded program?
by Anonymous Monk on May 05, 2014 at 11:35 UTC

    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

Re^3: What is the correct way to finish multithreaded program?
by moritz (Cardinal) on May 05, 2014 at 11:37 UTC
      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...
        If you can make the code run .. and reproduce the problem, you can see the only real way to wait is to join everything not merely the immediately joinable :)
        Here you go ... just because queue is empty, doesn't mean threads are joinable

        This reliably exits with 6 running and unjoined on my old machine, YMMV

        I'm pushing undef into the queue with ... and I think this will cause thread to return:

        It will. Eventually. But there is no guarantee -- indeed it is most unlikely -- that all of the threads will: a) get a timeslice; b) receive the undef from the queue; c) act upon it and exit; before your main thread tries to join them.

        The whole point of join is that it waits for the threads to finish, thus ensuring that your main thread doesn't exit before your child threads are finished.

        It rarely makes any sense to turn it into a non-blocking operation by calling is_joinable before calling join.

        The are a few specialist uses for that, but in the case of your code it only guarantees that any thread that is slow to finish will not get joined before you terminate the main thread; hence the error message.


        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: note [id://1085040]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-08-22 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls