Try this.
#!/usr/bin/perl
use strict; use warnings;
use threads;
use Thread::Queue;
$SIG{ INT } = sub{
print "Interupted\n";
exit;
};
my $Q = new Thread::Queue;
for (my $i = 0; $i < 10; $i++) {$Q->enqueue(int rand 6)}
my @worker;
for (my $i = 0; $i < 4; $i++) {$worker[$i] = threads->create(\&worker,
+ $Q)}
for (my $i = 0; $i < 4; $i++) {$worker[$i]->join}
print STDERR "workers done\n";
sub worker {
my ($q) = @_;
my $tid = threads->tid;
while ($q->pending) {
my $job = $q->dequeue;
print STDERR "processing sleep($job) in thread $tid\n";
sleep($job);
}
}
END {
print STDERR "END block executed\n";
}
__END__
c:\test>560021.pl
processing sleep(0) in thread 1
processing sleep(4) in thread 1
processing sleep(1) in thread 2
processing sleep(0) in thread 3
processing sleep(1) in thread 3
processing sleep(4) in thread 4
processing sleep(3) in thread 2
processing sleep(5) in thread 3
processing sleep(3) in thread 1
processing sleep(2) in thread 2
Interupted
END block executed
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.