Original Post
Happy Thanksgiving! Design Question: I have an EventTimer class that stores a priority queue of TimedEvent objects sorted by the time they are scheduled to be triggered. It spawns a thread in the constructor that sends the events off to the main handler, sleeping in between scheduled events. I originally intended this class to be a monolithic, "All your timers are belong to me", sort of object. And it is. But there's some problems with it. Recurring events will go on for the life of the timer, and if an Event is added while the monster is sleeping, it will be queued but ignored until the thread wakes. (imagine it's sleeping for the next event in 5s, and you add something that's supposed to go off in 1s). So. Is there any way to salvage this beast? My thoughts: Yes, but it's complicated. Recurring events could be issued IDs that can be used to cancel them later on. I could do some sort of fancy blocking scheme where the first thread blocks on a conditional, a 2nd thread sleeps until the next known event, and then either the addition of a 'sooner' TimedEvent -or- the 2nd thread waking will unblock the 1st thread. In short, NO. I will probably strip out the queue, and have a timer object track 1 event in 1 thread. Each timer will be local, be destroyed with it's owner, and the lifespan of recurring events will be naturally limited. No conflicts over sleeping threads. Your thoughts: Would love to hear them. Source is GPLv3 and WIP, check it out here if you like. Event.h Event.cpp (scroll to the bottom to find the EventTimer class) Cheers!