Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: END block not excuting when thread interrupted

by iankorf (Acolyte)
on Jul 09, 2006 at 22:28 UTC ( #560021=note: print w/replies, xml ) Need Help??


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";
}
  • Comment on Re^2: END block not excuting when thread interrupted

Replies are listed 'Best First'.
Re^3: END block not excuting when thread interrupted
by BrowserUk (Patriarch) on Jul 10, 2006 at 00:09 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://560021]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-12-01 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?