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
-
-
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.