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

threads eval/alarm problem

by fxmakers (Friar)
on Mar 27, 2004 at 00:39 UTC ( #340175=perlquestion: print w/replies, xml ) Need Help??

fxmakers has asked for the wisdom of the Perl Monks concerning the following question:


I'm having some troubles using eval and alarm in threads:
Code follows:
use threads; my $thread = threads->new(\&mysub, $var1, $var2); for (;;) { # here is an infinite loop # print seconds to see where it freezes } sub mysub { my ($var1, $var2) = (shift, shift); eval { local $SIG{'ALRM'} = sub { die("stop\n"); }; alarm(10); while (1) { # infinite loop } alarm(0); }; if ($@) { print "$@\n"; } return; }
The thread is created and runs fine until the 10sec timeout.
When this amount of time is reached, the whole script freezes, and 2 or 3 sec after, it crashes displaying "Alarm clock".
Nothing is in $@.

perl -v: This is perl, v5.8.2 built for cygwin-thread-multi-64int

Any ideas?

Edited by Chady -- added readmore tags.

Replies are listed 'Best First'.
Re: threads eval/alarm problem
by liz (Monsignor) on Mar 27, 2004 at 12:08 UTC
    Apart from the thinko:
    while (1) { # infinite loop }
    which should of course be:
    while (1) { } # infinite loop
    I think you should forget about using signals and threads together. See some of the caveats that I've documented with Thread::Signal. Basically, signals are very OS dependent, and within an OS, very version dependent. Mix in threads, which are also very OS dependent, and within an OS, very version dependent. Put Perl ithreads on top of that, trying to give a consistent interface to this, and you have a 3-dimensional matrix of trouble.

    If this is a program that you alone are going to be using, you may get away with it. But be prepared for some serious woes if you decide to either upgrade Perl, your OS or your threading support.


Re: threads eval/alarm problem
by diotalevi (Canon) on Mar 27, 2004 at 06:15 UTC
    You didn't share the variables that you're using in both threads.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://340175]
Approved by bart
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2022-12-03 16:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found