#!/usr/bin/perl use warnings; use strict; use threads; sub set_alarm { $SIG{'ALRM'} = sub { printf "Alarm went off in thread #%d\n", threads->tid(); print qq{"Get up, get up!", says the clock.\n}; exit 1; }; alarm shift; } # If you set the SIGALRM handler HERE and ... # If you join the thread, the alarm handler goes off after 10 seconds # ( The 'sleep' delay set inside the child thread. ) # If you detach the thread, the alarm handler goes off after 3 seconds # ( The 'alarm' delay set inside the thread ) # Or If you don't set 'alarm' inside the thread, the outer 'alarm' # delay is used. set_alarm( 5 ); my $sleepy = threads->create ( sub { # If the SIGALRM handler is used in this thread # (comment out the set_alarm above) # it simply prints "Alarm clock" ! hahahaha set_alarm( 3 ); sleep 10; print "Well, well, are you joining me in bed?\n"; } ); printf "Main thread # is %d.\nChild thread # is %d.\n", threads->tid(), $sleepy->tid(); # Try commenting out detach/sleep and uncommenting join # $sleepy->join; $sleepy->detach; sleep 15; # you only need to sleep when detach-ing print "*Fart*\n"; # Luckily this never gets reached... print "Oh crap I'm late for work!\n";