in reply to Thread::Pool shutdown dies after abort
A simple, working solution:
#! perl -slw use strict; use threads; use Thread::Queue; $|++; sub worker { my( $Q ) = @_; while( my $workitem = $Q->dequeue ) { print "Processing $workitem"; sleep rand 10; } } our $THREADS //= 2; my $Q = new Thread::Queue; $SIG{'INT'} = sub{ print "Sigint seen"; $Q->dequeue while $Q->pending; $Q->enqueue( (undef) x $THREADS ); }; $Q->enqueue( <DATA> ); my @threads = map threads->new( \&worker, $Q ), 1 .. $THREADS; $Q->enqueue( (undef) x $THREADS ); sleep 1 while $Q->pending; $_->join for @threads; __DATA__ WorkItem 1 WorkItem 2 WorkItem 3 WorkItem 4 WorkItem 5 WorkItem 6 WorkItem 7 WorkItem 8 WorkItem 9 WorkItem 10
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Thread::Pool shutdown dies after abort
by locked_user sundialsvc4 (Abbot) on Jun 30, 2011 at 01:35 UTC | |
by Anonymous Monk on Jul 04, 2011 at 14:48 UTC | |
by BrowserUk (Patriarch) on Jul 04, 2011 at 15:40 UTC |