On Wed, Sep 10, 2003 at 04:29:52AM +0200, Branko ??ibej wrote:
> >
> >wait(cond, mutex):
> > # section 1:
> > # wait for pending signals to be delivered
> > while 1:
> > acquire(cond->mutex)
> > if !cond->num_undelivered:
> > break
> > release(cond->mutex)
> >
> >
> And here's the problem. This is a busy-wait loop that'll drive the CPU
> load to 100%, and you most definitely don't want condition variables to
> behave that way.
>
True; it assumes that the scheduler won't grant the mutex to the same
thread again and again when other threads are ready to run.
Since Win32 has "counting semaphores", another approach would be to two
semaphores and a mutex instead of an event and a mutex. This may well be
overkill.
wait(cond,mutex):
acquire(cond->mutex)
cond->num_waiters++
release(cond->mutex)
release(mutex)
down(cond->sem)
up(cond->ack)
acquire(cond->mutex)
cond->num_waiters--
release(cond->mutex)
acquire(mutex)
signal(cond):
acquire(cond->mutex)
up(cond->sem,min(1, cond->num_waiters))
down(cond->ack,min(1, cond->num_waiters))
release(cond->mutex)
broadcast(cond):
acquire(cond->mutex)
up(cond->sem,cond->num_waiters)
down(cond->ack,cond->num_waiters);
release(cond->mutex)
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Wed Sep 10 14:24:02 2003