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

Re: [OT] Win32 condition variable implementation

From: Alon Ziv <alonz_at_wonder.nolaviz.org>
Date: 2003-09-12 10:52:25 CEST

Well, I'm humbled :-)

How about the following (pseudo-Python again :-)):

wait(cond,mutex):
  e = new event
  lock(cond.mutex)
  push(e.waiters, e)
  unlock(cond.mutex)
  SignalObjectAndWait(mutex, e)
  lock(mutex)

signal(cond):
  lock(cond.mutex)
  e = shift(e.waiters)
  unlock(cond.mutex)
  SetEvent(e)

broadcast(cond):
  lock(cond.mutex)
  waiters = e.waiters
  e.waiters = []
  unlock(cond.mutex)
  for e in waiters:
    SetEvent(e)

It's simpler than the version in the article you linked (sorry, I forgot the
link), as well as not blocking the broadcaster until the last thread is
awakened. The list of waiters can be implemented as a linked list of stack-
based structures to avoid any dynamic memory requirements.

The "SignalObjectAndWait" can also be replaced by
  unlock(mutex)
  WaitEvent(e)
which will still work on older Win32, and will not have the "lost wakeup"
problem (since I use only manual-reset events).

So, am I right or am I right? ;-)

        -az

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@subversion.tigris.org
For additional commands, e-mail: dev-help@subversion.tigris.org
Received on Fri Sep 12 16:34:54 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.