View mode: basic / threaded / horizontal-split · Log in · Help
September 30, 2007
Condition variables?
Hopefully I'm missing something obvious here, but D and phobos seem to
be missing any kind of condition variables.  It's really hard to do
non-trivial thread programming without this kind of synchronization.

In fact, I'm not sure how I could even go about implementing
something, since there doesn't seem to be any way of easily accessing
the object's monitor, which would be needed to do condition variables
that work with 'synchronized'.

I can think of other ways of doing synchronization, but not in a
terribly efficient way:

 - Use Thread's pause() and resume().  I would have to implement wait
   queues and getting synchronization right on this would be
   challenging.

 - Use another OS mechanism such as pipes to sleep and wakeup.  This
   is also not very efficient.

I'm just kind of wondering why std.thread even exists without
condition variables, since it really isn't useful for all that much,
by itself, and doesn't seem to even have the needed hooks to implement
any other mechanism.

David Brown
September 30, 2007
Re: Condition variables?
On 9/30/07, David Brown <dlang@davidb.org> wrote:
> Hopefully I'm missing something obvious here, but D and phobos seem to
> be missing any kind of condition variables.

I'm not sure what this has to do with D. It's a platform thing, not a
language thing. Linux has condition variables; Windows doesn't.
Windows has Events, Linux doesn't. What you're talking about is the
pthreads (posix threads) library. That's written in C, so you should
be able to call all the functions in it no problem - but don't expect
your program to run on Windows.

Exactly the same problem exists in C or C++, which is why I said it's
not a language thing.
September 30, 2007
Re: Condition variables?
David Brown wrote:
> Hopefully I'm missing something obvious here, but D and phobos seem to
> be missing any kind of condition variables.  It's really hard to do
> non-trivial thread programming without this kind of synchronization.
:snip:
> David Brown

What does one need condition variables for?
I'm honestly curious. I've written a few multithreaded programs in D
and, so far, haven't needed them :)

--downs
September 30, 2007
Re: Condition variables?
That probably ought to be part of the library ... in Tango it lives in 
tango.core.sync.Condition.

Using pause() and resume() is prone to deadlock, so it's surprising that 
they exist in Phobos

- Kris


"David Brown" <dlang@davidb.org> wrote in message 
news:20070930045805.GA2735@a64.davidb.org...
> Hopefully I'm missing something obvious here, but D and phobos seem to
> be missing any kind of condition variables.  It's really hard to do
> non-trivial thread programming without this kind of synchronization.
>
> In fact, I'm not sure how I could even go about implementing
> something, since there doesn't seem to be any way of easily accessing
> the object's monitor, which would be needed to do condition variables
> that work with 'synchronized'.
>
> I can think of other ways of doing synchronization, but not in a
> terribly efficient way:
>
>  - Use Thread's pause() and resume().  I would have to implement wait
>    queues and getting synchronization right on this would be
>    challenging.
>
>  - Use another OS mechanism such as pipes to sleep and wakeup.  This
>    is also not very efficient.
>
> I'm just kind of wondering why std.thread even exists without
> condition variables, since it really isn't useful for all that much,
> by itself, and doesn't seem to even have the needed hooks to implement
> any other mechanism.
>
> David Brown
September 30, 2007
Re: Condition variables?
On 9/30/07, downs <default_357-line@yahoo.de> wrote:
> What does one need condition variables for?
> I'm honestly curious. I've written a few multithreaded programs in D
> and, so far, haven't needed them :)

I would guess you're a Windows programmer?

You don't need them in Windows. Windows has plenty of other mechanisms
for doing synchronisation. Condition variables is "the linux way".

Still, I've never really got the hang of them either, so I'd love for
David to explain further.

In any case, I don't think they could be put into Phobos except as a
wrapper around pthreads ... which doesn't exist on Windows. Oh what
joy.
September 30, 2007
Re: Condition variables?
On 9/30/07, David Brown <dlang@davidb.org> wrote:
> It's really hard to do
> non-trivial thread programming without this kind of synchronization.

When I first tried to do thread programming on Linux I was baffled by
the absence of Events or anything similar. But surely, I said, it's
really hard to do non-trivial thread programming without this kind of
synchronization. Aha!, I was told, Linux has these things called
condition variables instead. (I still haven't quite got the hang of
them as I'm basically a Windows person).


> I'm just kind of wondering why std.thread even exists without
> condition variables, since it really isn't useful for all that much,
> by itself, and doesn't seem to even have the needed hooks to implement
> any other mechanism.

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?

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).
September 30, 2007
Re: Condition variables?
Janice Caron wrote:
> On 9/30/07, downs <default_357-line@yahoo.de> wrote:
>> What does one need condition variables for?
>> I'm honestly curious. I've written a few multithreaded programs in D
>> and, so far, haven't needed them :)
> 
> I would guess you're a Windows programmer?
> 
Started out on linux, but all my D code runs cross-platform.

> You don't need them in Windows. Windows has plenty of other mechanisms
> for doing synchronisation. Condition variables is "the linux way".
> 
Yes, but I still don't see what thread synch you need them for that
can't be done in a few lines of synchronized() code.

> Still, I've never really got the hang of them either, so I'd love for
> David to explain further.
> 
> In any case, I don't think they could be put into Phobos except as a
> wrapper around pthreads ... which doesn't exist on Windows. Oh what
> joy.
Joy! ;)

--downs
September 30, 2007
Re: Condition variables?
On 30/09/2007, Janice Caron <caron800@googlemail.com> wrote:
> On 9/30/07, David Brown <dlang@davidb.org> wrote:
> > Hopefully I'm missing something obvious here, but D and phobos seem to
> > be missing any kind of condition variables.
>
> I'm not sure what this has to do with D. It's a platform thing, not a
> language thing. Linux has condition variables; Windows doesn't.
> Windows has Events, Linux doesn't. What you're talking about is the
> pthreads (posix threads) library. That's written in C, so you should
> be able to call all the functions in it no problem - but don't expect
> your program to run on Windows.
>
> Exactly the same problem exists in C or C++, which is why I said it's
> not a language thing.

Hi Janice,

Condition variables are a much more theoretical concept than simply
"implemented in POSIX", and FWIW were finally added to Vista:

  http://msdn.microsoft.com/msdnmag/issues/07/06/Concurrency/default.aspx

Quoth:

  The condition variable has existed in other threading libraries for some
  time and has been sorely missed from the Windows SDK.

There's a hundred and one different things in the text books
implemented in terms of them, Windows just took the approach of
providing abstractions instead.


David.

>
September 30, 2007
Re: Condition variables?
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
September 30, 2007
Re: Condition variables?
Janice Caron wrote:
> On 9/30/07, downs <default_357-line@yahoo.de> wrote:
>> What does one need condition variables for?
>> I'm honestly curious. I've written a few multithreaded programs in D
>> and, so far, haven't needed them :)
> 
> I would guess you're a Windows programmer?
> 
> You don't need them in Windows. Windows has plenty of other mechanisms
> for doing synchronisation. Condition variables is "the linux way".
> 
> Still, I've never really got the hang of them either, so I'd love for
> David to explain further.
> 
> In any case, I don't think they could be put into Phobos except as a
> wrapper around pthreads ... which doesn't exist on Windows. Oh what
> joy.

No?

http://sourceware.org/pthreads-win32/

Roald
« First   ‹ Prev
1 2 3 4
Top | Discussion index | About this forum | D home