bash $ (sleep 1; killall -HUP the_parent) & perl threads.pl [1] 4842 Thread 1: Created Thread 2: Created Thread 3: Created Shutting down 3 threads Thread 1: Shutting down Thread 3: Shutting down 1 still running. Giving up! #### #!/usr/bin/env perl use 5.012; use warnings FATAL => 'all'; use autodie; if (my $pid = fork) { $0 = 'the_parent'; local $SIG{HUP} = sub { kill USR1 => $pid }; exit(0 < waitpid $pid,0); } $0 = 'the_child'; use threads; use threads::shared; my $shutdown :shared = 0; my @thr = map { threads->create(sub { printf "Thread %d: Created\n", threads->tid; sleep 1 + rand 2 until $shutdown; printf "Thread %d: Shutting down\n", threads->tid; lock($shutdown); cond_signal($shutdown); }) } 1..3; $SIG{USR1} = sub { $shutdown = 1 }; sleep until $shutdown; say "Shutting down " . scalar threads->list . " threads"; my $until = 1 + time; # Wait this many seconds before abort while (my $threads = threads->list(threads::running)) { lock($shutdown); cond_timedwait($shutdown, $until) or last; } if (my @remain = threads->list(threads::running)) { $_->detach for @remain; $_->join for threads->list(threads::joinable); die scalar @remain . " still running. Giving up!\n"; } $_->join for @thr; say "All threads exited normally.";