#!/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; }