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

Re: threading - Waiting for multiple notifications



If I were doing this, I would have a single scheduler thread with
a single "something changed" notification.  This thread calls 'wait-for'
with the computed timeout.  The scheduler object has a slot that
contains a queue of "things to do", plus an API to add something
to the queue.  If the 'wait-for' completes and the queue is empty,
you assume that the timer went off, so you go run things that need
to be run.  Otherwise you look at the contents of the queue and
do what you need to do there.

The WM_TIMECHANGED handler then just does the "usual thing"
of adding a "time changed" event to the *head* of the queue, and
releases the notification.

The first paragraph seems to describe what you are describing, so
I have to wonder my second paragraph is so divergent from your
scheme.  I assume that either you missed something obvious in your
implementation, or that I missed something obvious in your explanation
or my design.

It's worth looking at duim/sheets/event-queue.dylan and the methods
on <sheet-with-event-queue-mixin> in duim/sheets/events.dylan,
since they use the FD thread model "as intended".  Note particularly
the use of 'release-all'; it's a trap in the threads API that you almost
always want to use 'release-all' instead of 'release' (I would have
named them 'release' and 'release-one', but it was too much trouble
to press the argument.)

"Chris Double" <chris@double.co.nz> wrote in message
wkofo1jd7x.fsf@double.co.nz">news:wkofo1jd7x.fsf@double.co.nz...
> 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 ...
>
> 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: References: