Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: More than one alarm-call at a time

by bluto (Curate)
on Jan 16, 2004 at 17:33 UTC ( [id://321843]=note: print w/replies, xml ) Need Help??


in reply to More than one alarm-call at a time

You could write a wrapper that remembers the list of alarms that need to trigger and then just run alarm() on the one that is the closest in the future. Something (untested) like ...

{ 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://321843]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found