http://qs321.pair.com?node_id=560021


in reply to Re: END block not excuting when thread interrupted
in thread END block not excuting when thread interrupted

Here's a simple code snippet that replicates the behavior. The program creates a job queue with 10 jobs and has 4 workers act on those. The jobs are sleeps from 0-5 sec. If you let the program run to completion, it prints "workers done" and then "END block executed". If you interrupt it with ^C you get nothing, not even an error message.
#!/usr/bin/perl
use strict; use warnings;
use threads;
use Thread::Queue;

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";
}