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:
But I sense that you will not be satisfied with simple, so good luck :)
|
|---|