Condition variables?

David Brown dlang at davidb.org
Sat Sep 29 23:33:09 PDT 2007


On Sun, Sep 30, 2007 at 06:55:33AM +0100, Janice Caron wrote:

> Yes, I certainly agree that std.thread is limited - it doesn't have
> condition variables /or/ Events, and surely you need one or the other?

I think I now understand why this has been left out of the thread package.
The problem is that under Linux, there isn't any way to properly use
conditions.  I can either make my own mutexes, and ignore the
'synchronized' construct, or hack into the object representation to use it
myself.

> For that matter it doesn't even have mutexes (unless you count the
> ones built into every Object, and they're not necessarily the right
> tool for every job).

Interestingly, Microsoft has chosen condition variables for synchronization
in C#.  I have heard hearsay that programming with condition variables is
less prone to problems than events.  My experience is that people can
easily mess up with either.

It would be nice to have something like condition variables implemented in
Phobos that would work portably across multiple platforms.  I think most
platforms other than windows will already have something similar to
condition variables, since it is what Posix defines.

I guess this is only a problem because I'm liking what 'synchronized'
provides, in that it releases the mutex no matter what.  Without it, I'm
left having to do something like (off of the top of my head).

   {
      mutex.lock ();
      scope (exit) mutex.unlock ();

      ...operation...
   }

which looks a bit odd, although less odd than most other languages.

Dave



More information about the Digitalmars-d mailing list