http://qs321.pair.com?node_id=340175

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

Hi!

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?
Thanks!

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.

    Liz

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.