Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: about THREAD SIGNALLING

by Khen1950fx (Canon)
on Apr 23, 2013 at 06:26 UTC ( #1030046=note: print w/ replies, xml ) Need Help??


in reply to about THREAD SIGNALLING

I believe you were looking for this:

#/usr/bin/perl use strict; use warnings; use threads; use threads::shared; my @thrList; my $count = 1; share $count; my $counter = 3; while($counter) { $counter--; my $thrHandle = threads->create(\&thrdfun); push @thrList, $thrHandle; } foreach(@thrList){ $_->join(); } sub thrdfun { sub catch_CtrlC { $SIG{'INT'} = threads->kill('TERM'); } $SIG{INT} = \&catchCtrlC; print "thrdfun: $count \n"; $count++; while(1){ (); } exit; }


Comment on Re: about THREAD SIGNALLING
Download Code
Re^2: about THREAD SIGNALLING
by anaconda_wly (Scribe) on Apr 23, 2013 at 06:55 UTC
    OK. I'd like to find out why my code doesn't work. I found $SIG{'INT'} = threads->kill('TERM');changed to
    threads->kill('TERM');
    still works.

    Then I made a little more change to you code as:

    #/usr/bin/perl use strict; use warnings; use threads; use threads::shared; $SIG{INT} = \&catchCtrlC; my @thrList; my $count = 1; share $count; my $counter = 3; while($counter) { $counter--; my $thrHandle = threads->create(\&thrdfun); push @thrList, $thrHandle; } foreach(@thrList){ $_->join(); } sub catch_CtrlC { foreach(@thrList){ $_->kill('TERM'); } } sub thrdfun { print "thrdfun: $count \n"; $count++; while(1){ (); } exit; }

    It doesn't work. Could you tell me why?

      What's more, can catch_CtrlC gets parameters? Why below little change, adding a print in catch_CtrlC doesn't print anything(see line print "counter: $counter \n"; )?
      #/usr/bin/perl use strict; use warnings; use threads; use threads::shared; my @thrList; my $count = 1; share $count; my $counter = 3; while($counter) { $counter--; my $thrHandle = threads->create(\&thrdfun); push @thrList, $thrHandle; } foreach(@thrList){ $_->join(); } sub thrdfun { sub catch_CtrlC { print "counter: $counter \n"; $SIG{'INT'} = threads->kill('TERM'); } $SIG{INT} = \&catchCtrlC; print "thrdfun: $count \n"; $count++; while(1){ (); } exit; }
      Just remember that sub catch_CtrlC {} and SIG{'INT'} = \&catch_CtrlC; must both be included in each thread that you want to SIGINT out of.
      #/usr/bin/perl use strict; use warnings; use threads; use threads::shared; my @thrList; my $count = 1; share $count; my $counter = 3; while ($counter) { $counter--; my $thrHandle = threads->create( \&thrdfun ); push @thrList, $thrHandle; } foreach (@thrList) { $_->join(); } sub thrdfun { sub catch_CtrlC { $_->kill('TERM'); } $SIG{'INT'} = \&catch_CtrlC; print "thrdfun: $count \n"; $count++; while (1) { (); } exit; }
        OK, I got the point. The thread sig handler should be defined inside the thread body, different from the process signal handler.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-09-01 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls