Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Handling SIG INT multiple times

by mr_ron (Chaplain)
on Oct 02, 2015 at 13:52 UTC ( [id://1143661]=note: print w/replies, xml ) Need Help??


in reply to Re: Handling SIG INT multiple times
in thread Handling SIG INT multiple times

In theory and somewhat in practice Perl 6 neatly handles this for you with Supplies and taps handling signals in a thread safe sort of way. See "System events exposed as Supplies" in the Perl 6 design documents. The Perl 6 solution inspired me to write some Perl 5 code with similar capability that I have included below. Note that like the Perl 6 solution my code will "as is" do the actual handling of SIGINTs sequentially but it should be quite easy to add some parallel threads if wanted.

use strict; use warnings; use threads; use Thread::Queue; my $q = Thread::Queue->new(); # A new empty queue my $thr = threads->create( sub { # Thread will loop until no more work while (defined(my $item = $q->dequeue())) { print "starting work on $item\n"; sleep 5; print "finished work on $item\n"; } } ); my $item = 1; { local $SIG{INT} = sub { $q->enqueue($item++) }; kill 'INT', $$; sleep 2; kill 'INT', $$; sleep 2; } $q->enqueue(undef); # I think in more modern Thread::Queue has $q->end $thr->join();
Ron

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-25 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found