[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

[OT] Win32 condition variable implementation

From: mark benedetto king <mbk_at_lowlatency.com>
Date: 2003-09-10 14:22:48 CEST

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

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.