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


In reply to Re: More than one alarm-call at a time by bluto
in thread More than one alarm-call at a time by htoug

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.