#!/usr/bin/perl use strict; use warnings; use threads; use Time::HiRes qw(setitimer time ITIMER_REAL); my $NUM = 5; # install main signal handler before creating threads $SIG{ALRM} = sub { local $|=1; print time, ": Caught SIGALRM in main\n" }; setitimer(ITIMER_REAL, 0.1, 0.1); # create all the threads my $tstart = time; threads->create("run_thread", $_) for (1 .. $NUM); my $tdone = time; printf "%d threads in %f seconds == %d threads/second\n\n", $NUM, $tdone - $tstart, $NUM / ($tdone - $tstart); sleep 1 while 1; sub run_thread { my $num = shift; $SIG{ALRM} = sub { local $| = 1; print time, ": Caught SIGALRM in thread $num\n"; print "\n" if $num == $NUM; }; setitimer(ITIMER_REAL, 0.1, 0.1); sleep 1 while 1; } #### 5 threads in 0.079028 seconds == 63 threads/second 1062803574.97634: Caught SIGALRM in main 1062803574.99621: Caught SIGALRM in thread 1 1062803575.02622: Caught SIGALRM in thread 2 1062803575.02631: Caught SIGALRM in thread 3 1062803575.04619: Caught SIGALRM in thread 4 1062803575.05619: Caught SIGALRM in thread 5 1062803575.07617: Caught SIGALRM in main 1062803575.09617: Caught SIGALRM in thread 1 1062803575.12616: Caught SIGALRM in thread 2 1062803575.12621: Caught SIGALRM in thread 3 1062803575.14616: Caught SIGALRM in thread 4 1062803575.15617: Caught SIGALRM in thread 5