in reply to More than one alarm-call at a time
{ my @alarm_times; sub myalarm { my $new_alarm = shift; if (defined $new_alarm) { if ($new_alarm) { push @alarm_times, time + $new_alarm; @alarm_times = sort { $a <=> $b} @alarm_times; } else { @alarm_times = (); } } while (my $t = shift @alarm_times) { next if $t < time; alarm($t - time); return; } alarm(0); } ... myalarm(10); # add alarm 10 seconds in future myalarm(0); # cancel all alarms myalarm; # set up next alarm after one triggers
Your alarm signal handler will have to reestablish the next alarm to run by calling this routine without arguments. Note: if this routine is being called when another alarm occurs, you are probably in trouble. Even if you put something like 'alarm(0)' at the beginning of the routine, you will probably miss alarms. For this reason and other strange issues related to signals (i.e. sporadic core dumps), if at all possible, you may want to consider redesigning this since you are pushing the limits on what alarms were intended for.
bluto
|
|---|