in reply to Catching signals under Linux

On a more elementary level, why does this code:

use strict; use warnings; use 5.010; use threads; sub thr_func { say 'in thread'; $SIG{'HUP'} = sub { say "signal caught" }; } my $thr = threads->create('thr_func'); $thr->kill('HUP')->join();
produce this output:
Signal SIGHUP received, but no signal handler set.

The threads docs seem pretty clear that you can signal a thread.

Replies are listed 'Best First'.
Re^2: Catching signals under Linux
by JavaFan (Canon) on Jan 29, 2010 at 10:57 UTC
    Because you're likely to send the signal before the other thread was able to set the handler. Note that your output doesn't include 'in thread', which is printed before you set SIGHUP.

      I have not looked closely at your code, but I would observe that the scenario just described is extremely common, and therefore extremely likely.

      Yup:
      use strict; use warnings; use 5.010; use threads; sub thr_func { say 'in thread'; $SIG{'HUP'} = sub { say "signal caught" }; say 'doing some task...'; sleep 5; } my $thr = threads->create('thr_func'); sleep 2; $thr->kill('HUP')->join(); --output:-- in thread doing some task... signal caught
      Thanks.