Path: secnews.netscape.com!rls@netscape.com
From: Rob McCool
Newsgroups: netscape.devs-nsapi
Subject: Re: How to use CONDVARs
Date: Thu, 19 Dec 1996 13:12:43 -0800
You will want to use two critical sections, one for the worker CV, one
for the done CV. I don't think you can do it with one.
This is off the top of my head so it may be wrong, but the code would
then look like this:
> THREADS 1-N
>
> if (sufficient work for worker) {
> crit_enter(c_done);
> crit_enter(c_worker);
> condvar_notify(cv_worker);
> crit_exit(c_worker);
> condvar_wait(cv_done);
> crit_exit(c_done);
> }
> process request;
>
> WORKER
>
> for(;;) {
> crit_enter(c_worker);
> condvar_wait(cv_worker);
> crit_exit(c_worker);
> do work;
> crit_enter(c_done);
> condvar_notify(cv_done);
> crit_exit(c_done);
> }
Because the notifying thread is holding the done critical section, the
worker will not be able to notify it that the work is complete until
the thread waits for the done condvar. This should avoid the race
condition.
--
Rob McCool, robm@netscape.com http://home.netscape.com/people/robm/
Stunt Programmer, Netscape Communications Corporation
It was working ten minutes ago, I swear...
Reproduced by permission of the author.