Posted on 2009-05-07 20:49:26-07 by mmatchyn
Threads join leaves threads running
Often times I have threads that do not finish after the join method is called. I thought join was supposed to wait for all threads to finish running. When I create 9 threads I often get the following messages: 5 running and unjoined 4 finished and unjoined 0 running and detached It skips 5 when that message comes back. If I get this message it is ok: 0 running and unjoined 9 finished and unjoined 0 running and detached This is what the code looks like
for (1-10) { $thr=threads->create(\&func,$vars); } $thr->join();
Is there a way I can make join wait until all 10 threads finish? I would like to see the 2nd set of output statements every time it runs. Instead sometimes I get the first set which causes problems and other times I get the 2nd set which is ok. I need to ensure all threads ran properly before exiting. I am using: Perl v5.8.5 threads : 1.72 threads::shared : 0.92
Direct Responses: 10646 | Write a response
Posted on 2009-05-08 12:10:04-07 by jdhedden in response to 10638
Re: Threads join leaves threads running
$thr->join() only joins the thread for object $thr. In your loop, $thr is assigned to the next thread with each iteration.

If you don't need to know anything about the threads other than to join them, you can simply your code to this:
threads->create(\&func, $var) foreach 1..10; $_->join() foreach threads->list();
(Of course, there are other ways you could do this.)
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.