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

Re: Why does threads::join block SIGALRM

by SimonPratt (Friar)
on Jun 07, 2016 at 09:33 UTC ( [id://1165057]=note: print w/replies, xml ) Need Help??


in reply to Why does threads::join block SIGALRM

A workaround would be to not use the blocking function of join. Something like this would work:

#!/usr/bin/env perl use strict; use warnings; use threads; sub worker { print "Worker thread started.\n"; while(1){} } my $thread = threads->create(\&worker); print "Setting alarm.\n"; $SIG{ALRM} = sub { die "Alarm!\n" }; alarm 2; print "Check join\n"; while (threads::running) { $_->join for threads->list(threads::joinable); }

Yes, I am aware that this will spin like crazy, wasting a lot of system resources, however you can't sleep to limit this, as sleep is sometimes implemented using the system alarm. Actually thinking about it some more, I suspect that join may have been modified to use the system alarm, hence wiping out the alarm you initially set, causing it to never trigger.

Based on this, a "better" approach could be something like this:

#!/usr/bin/env perl use strict; use warnings; use threads; use constant TIMEOUT => 2; sub worker { print "Worker thread started.\n"; while(1){} } my $thread = threads->create(\&worker); print "Check join\n"; while (1) { $_->join for threads->list(threads::joinable); last unless threads->list(threads::running); die "Alarm!\n" if time - $^T > TIMEOUT; select(undef,undef,undef,0.5); }

Update: Updated second code example

Replies are listed 'Best First'.
Re^2: Why does threads::join block SIGALRM
by CDahn (Novice) on Jun 08, 2016 at 02:23 UTC
    Yeah, I refactored the code today to effectively the same thing you suggested in the second code example above. It doesn't have the same "instant" granularity of interrupting the join(), but I would prefer to do it this way than run with unsafe signals. Thanks!

Log In?
Username:
Password:

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

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

    No recent polls found