i have small sample program hangs on perl 5.16.3. attempting use alarm
trigger if 2 threads don't finish working in time in more complicated program, boils down gist of it. know there's plenty of other ways this, sake of argument, let's i'm stuck code way is. i'm not sure if bug in perl, or legitimately shouldn't work.
i have researched on internet, , seems mixing alarms , threads discouraged, i've seen plenty of examples people claim reasonable thing do, such other question, perl threads alarm. code provided in accepted answer on question hangs on system, why i'm wondering if maybe that's broke, @ least of 5.16.3.
it appears in code below, if call join
before alarm
goes off, alarm
never triggers. if replace join
while(1){}
, go busy-wait loop, alarm
goes off fine, appears join
blocking sigalrm reason.
my expectation join
happens, , few seconds later see "alarm!" printed on screen, never happens, long join
gets called before alarm
goes off.
#!/usr/bin/env perl use strict; use warnings; use threads; sub worker { print "worker thread started.\n"; while(1){} } $thread = threads->create(\&worker); print "setting alarm.\n"; $sig{alrm} = sub { print "alarm!\n" }; alarm 2; print "joining.\n"; $thread->join();
the problem has nothing threads. signals processed between perl ops, , join
written in c, signal handled when join
returns. following demonstrates this:
#!/usr/bin/env perl use strict; use warnings; use threads; sub worker { print "worker thread started.\n"; (1..5) { sleep(1); print(".\n"); } } $thread = threads->create(\&worker); print "setting alarm.\n"; $sig{alrm} = sub { print "alarm!\n" }; alarm 2; print "joining.\n"; $thread->join();
output:
setting alarm. joining. worker thread started. . . . . . alarm!
join
call pthread_join
. unlike other blocking system calls, pthread_join
not interrupted signals.
by way, renamed $tid
$thread
since threads->create
returns thread object, not thread id.