views:

115

answers:

1

I'm designing a system that needs timers at all levels of a component hierarchy. Multiple timers may be active at once, but they need to interact with each other (stopping a component's timer stops its descendants' timers, while starting a component's timer starts its ancestors' timers and stops its siblings' timers).

The timers each have different limits, and will be sending tick, start, stop, and expire events to listeners.

This is still early in the design phase (implementation language is still undetermined), but I want to avoid a threading mess. Any advice?

+2  A: 

It's hard to give advice based on what you've posted. Perhaps avoid threads completely and just have a single timer and database table of events. When the timer goes off, you select all the events older than now and fire them off. Then select the min timestamp of the event table subtract now and sleep for that interval. Loop.

while (1) {
    SELECT * FROM EVENTS WHERE ev_time < now() order by ev_time;
    while (num_rows_selected > 0) {
        for (i = 0; i < num_rows_select; ++i) {
             fetch row
             process row
        }
        SELECT * FROM EVENTS WHERE ev_time < now() order by ev_time;
    }
    SELECT MIN(ev_time)-NOW() as Interval FROM EVENTS
    sleep(Interval);
}
jmucchiello