Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

about THREAD SIGNALLING

by anaconda_wly (Scribe)
on Apr 23, 2013 at 05:43 UTC ( #1030040=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

I wrote sample code of thread control using signaling as advised by monks. As below, but the thread didn't exit when Ctrl+c pressed:
#/usr/bin/perl -w use strict; use warnings; use threads; use threads::shared; my @thrList; my $count=1; share $count; $SIG{INT} = \&catch_CtrlC; &main; sub main { my $counter = 3; while($counter) { $counter--; my $thrHandle = threads->create(\&thrdfun); push @thrList, $thrHandle; } print " waiting \n"; foreach(@thrList){ $_->join(); } } sub thrdfun { $SIG{INT} = \&trExit; print "thrdfun: $count \n"; $count++; while(1){ } exit; } sub trExit { print " exit \n"; threads->exit(); } sub catch_CtrlC { print " catch_CtrlC \n"; foreach(@thrList){ $_->kill("INT"); } exit; }
In my plan, catch_CtrlC will first receive the signal and forward it to all thread one by one. For every thread, the trExit will do sth. and exit. I guess I'm using in a wrong way, righ?

Comment on about THREAD SIGNALLING
Download Code
Re: about THREAD SIGNALLING
by Khen1950fx (Canon) on Apr 23, 2013 at 06:26 UTC
    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; }
      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; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-08-21 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (133 votes), past polls