Version française
Home     About     Download     Resources     Contact us    
Browse thread
Usage of condition variables
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: [Caml-list] Usage of condition variables
> [Use of condition variables]
> However it seems like members of OCaml team strongly prefer unlock
> then signal pattern. Given that Xavier Leroy is also the author of
> LinuxThreads there might be some good arguments to have it that way,
> or maybe it's because of vmthreads, perhaps someone could give the
> rationale?

That's an old debate -- I've seen threads of 100+ messages on this
topic back in the days when I read comp.programming.threads.

My take on this is that both patterns are correct if your program is
reasonable, i.e. no real-time scheduling with priorities, and all uses
of Condition.wait are protected against spurious wakeups (as they
should always be).

Moreover, the "unlock then signal" variant is slightly more efficient
with naive thread implementations such as LinuxThreads, because
"signal then unlock" can cause threads waiting on the condition to be
restarted only to block immediately on the mutex.  (More sophisticated
implementations can avoid this by clever manipulations of wait queues.)

Butenhof's book on POSIX threads probably contains a better discussion
of the issue.

- Xavier Leroy