Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: about THREAD SIGNALLING

by anaconda_wly (Scribe)
on Apr 23, 2013 at 06:55 UTC ( [id://1030050]=note: print w/replies, xml ) Need Help??


in reply to Re: about THREAD SIGNALLING
in thread about THREAD SIGNALLING

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?

Replies are listed 'Best First'.
Re^3: about THREAD SIGNALLING
by Khen1950fx (Canon) on Apr 23, 2013 at 08:26 UTC
    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.
Re^3: about THREAD SIGNALLING
by anaconda_wly (Scribe) on Apr 23, 2013 at 07:15 UTC
    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; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-26 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found