Is there a way to run independent timers without one persistent thread for each?

What I do in a somewhat similar situation is to have a timer interrupt every 1 or 2 seconds. The handler for that event (or signal) processes a list looking for work to do. In your case the list might be a hash with period and function entries where period would be how often to start the thread (when time % period is equal to 0, possibly taking into account the potential for missed events) and where function is the function to start the thread. Presumably you could have more entries in the hash including args to the function.

In my application in addition to period I put in the absolute time to start the function and use that instead of the period directly. Then if I miss a tick the start time is earlier than the clock so the routine needs to be started. I update the start time entry (by adding the period to the start time or current time) before actually calling the function.

HTH, --traveler


In reply to Re: multiple threads with different "timers" by traveler
in thread multiple threads with different "timers" by Anonymous Monk

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.