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

Re: END block not excuting when thread interrupted

by jdhedden (Deacon)
on Jul 09, 2006 at 17:31 UTC ( [id://560014]=note: print w/replies, xml ) Need Help??


in reply to END block not excuting when thread interrupted

First of all, try the latest version of threads. It handles thread exits in a more robust manner.

Also, please post some sample code so we can see just want you're doing.


Remember: There's always one more bug.
  • Comment on Re: END block not excuting when thread interrupted

Replies are listed 'Best First'.
Re^2: END block not excuting when thread interrupted
by iankorf (Acolyte) on Jul 09, 2006 at 22:28 UTC
    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";
    }
    

      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://560014]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-19 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found