in reply to Re^8: Why do my threads sometimes die silenty?
in thread Why do my threads sometimes die silenty?

There are issues that complicates things. ...

Oh. So you need bidirectional communications with your threads.

That complicates things -- but not so much:

#! perl -slw use strict; use threads; use Thread::Queue; sub worker { my( $Qwork, $Qback ) = @_; my $tid = threads->tid; print "Thread $tid started"; while( defined( my $work = $Qwork->dequeue ) ) { print "Thread $tid processing workitem $work"; $Qback->enqueue( $work * $tid ); sleep 1; } $Qback->enqueue( undef ); print "Thread $tid done"; } our $WORKERS //= 10; our $ITEMS //= 30; my $Qwork = new Thread::Queue; my $Qback = new Thread::Queue; my @pool = map threads->new( \&worker, $Qwork, $Qback ), 1 .. $WORKERS +; $Qwork->enqueue( 1 .. $ITEMS ); $Qwork->enqueue( (undef) x $WORKERS ); for( 1 .. $WORKERS ) { while( defined( my $res = $Qback->dequeue ) ) { print "Main retrieved result: $res"; } } print "Main done"; $_->join for @pool

A run:

c:\test>TQpool -WORKERS=4 -ITEMS=10 Thread 1 started Thread 2 started Thread 2 processing workitem 1 Thread 4 started Thread 4 processing workitem 3 Thread 3 started Main retrieved result: 2 Main retrieved result: 12 Thread 3 processing workitem 4 Main retrieved result: 12 Thread 1 processing workitem 2 Main retrieved result: 2 Thread 2 processing workitem 5 Main retrieved result: 10 Thread 4 processing workitem 6 Main retrieved result: 24 Thread 3 processing workitem 7 Main retrieved result: 21 Thread 1 processing workitem 8 Main retrieved result: 8 Thread 2 processing workitem 9 Main retrieved result: 18 Thread 4 processing workitem 10 Main retrieved result: 40 Thread 3 done Thread 1 done Thread 2 done Main done Thread 4 done

But I sense that you will not be satisfied with simple, so good luck :)


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.