[Prev][Next][Index][Thread]

threading - Waiting for multiple notifications



A questions on the threads library as implemented by Functional
Developer.

How can I wait on multiple <notification>'s? 

Here's the scenario. I have a scheduler object that runs scheduled
events at specific times or after a certain duration. The main
schedule loop looks at the list of items scheduled, and sleeps for the
number of seconds until the first scheduled item is to be run.

I don't use the 'sleep' call for this because I want to check if other
items are added to the schedule, or items have been removed, and if
so, break out of the sleep and re-calculate how long I have to sleep
for. 

To handle this I have a <notification> that is released when the
schedule is modified. My sleeping function does a wait-for on this
<notification> with a timeout set to the number of seconds to
sleep. If nothing is added to the schedule it times out and runs the
scheduled item. If something is added or modified, the notification is
caught and the seconds to sleep recalculated.

This workde fine but what if the user changes the system time? To
solve this I added routines that detect a change in the Windows system
time. This way I can detect if the user has changed the system time
and once again recalculate my sleep time. The routines have a
<notification> that is released when the WM-TIMECHANGED windows
message is broadcast (In the Dylanlibs CVS as win32-notifications if
interested).

Now I want to wait on the 'schedule modified' <notification> and the
'time changed' <notification> with a timeout. But the threads library
only allows waiting on one notification (I think, is that right?).

I'm solving the issue at the moment by running the wait-for on the two
notifications in separate threads. When the wait-for exits these
threads raise yet another <notification> to say 'wake up'. My main
scheduler loop then does a wait-for on the 'wake up'
notification. 

This works but has the problem of how to have the two wait-for threads
die nicely. That is, when the time-changed notification occurs, how do
I tell the schedule-changed wait-for thread to die and vice versa. To
solve this I keep both threads alive all the time. This could get
expensive if there are many different scenarios where I'm waiting on
multiple notifications.

Perhaps I'm doing this the hard way. Any other ideas or advice on how
to do this?

Note that my solution works and runs fine, but I'm interested if there
are better or more general ways of solving the problem.

Chris.
-- 
http://www.double.co.nz/dylan





Follow-Ups: