View Single Post
Old Sep 5th, 2007, 5:53 AM   #4
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 327
Rep Power: 3 kruptof is on a distinguished road
Quote:
Originally Posted by lectricpharaoh View Post
Let's say you've implemented this as a heap. Rather than storing a delay with your items, store the 'due time'. In other words, add the delay to the current time, derive a value from this, and use this as the key that determines the placement of your new item in the heap. This way, when enqueuing an item, it will only be on the top of the heap if it indeed must be the first event handled.

You can couple this with setting the timer to fire at only the required intervals with a bit of work, too, though it depends on the granularity you need. You also need to handle the situation where multiple events need 'simultaneous' handling, and by this I mean two or more events that need to be handled on the same firing of the timer. Basically, when the timer fires, you dequeue the top item, handle it, then peek at the next. If its timestamp indicates it needs handling too, you do so, and you continue on until the top element doesn't yet need handling.

This should handle recurring events too, though I kind of inferred you meant one-shot events. For recurring events, just enqueue them again after they get handled. With my suggestion about using due times rather than delays, coupled with handling multiple events per timer firing, lower-priority events should still be handled eventually.
Thanks lectricpharaoh, yeah I have implemented this using a min-heap. The switch from delay time to due time should definitely fix the problems I was having with low priority events, as for the multiples events occurring at the same time, I shall peek as suggested and take the appropriate action.
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote