Suggestion for Condition variables

Graham St Jack grahams at acres.com.au
Mon Feb 27 14:59:01 PST 2006


Oskar Linde wrote:
> Sean Kelly skrev:
> 
>> Graham St Jack wrote:
>>
>>> Here is a suggestion for an extension to D to handle pthread-like 
>>> condition variables in a way that fits neatly with the "synchronized" 
>>> keyword.
>>>
>>> I use threads a lot, and quite often find that I need conditions, but 
>>> even though all the functionality is there in the locks library, it 
>>> is galling that I can't use the same mutex as the "synchronized" 
>>> keyword.
>>>
>>> The alternatives seem to be to:
>>> * Eliminate or ignore the synchronized keyword and use a library all 
>>> the   time.
>>> * Make the language "aware" of the library, and provide a way of 
>>> getting an object's mutex.
>>> * Add some more keywords to handle conditions directly.
>>
>>
>> I've been thinking about this as well, and I think it helps that the 
>> monitor location is already specified in the ABI.  This should allow 
>> library use of monitors whether there's any official language support 
>> or not.
>>
>>> My suggestion is for the last, because I like synchronized, and 
>>> because I don't like making the language aware of the libraries.
>>>
>>>
>>> So, the suggestion is to add:
>>>
>>> * A "condition" keyword that behaves for the most part just like a 
>>> bool data type, but is actually a condition variable.
>>
>>
>> Personally, I'd be happy with library code for this, so long as it 
>> could use the built-in monitors.  I'd also like a bit more flexibility 
>> with how locks are obtained and such, but perhaps this is better left 
>> for later.
> 
> 
> Any comments on Tommie Gannerts implementation?
> http://www.digitalmars.com/d/archives/digitalmars/D/31340.html
> 
> /Oskar

The patch neatly implements a fourth option - give a library back-door 
access to an object's mutex. While I haven't tried it out, it achieves 
what I need very well.

I do have a preference for an addition to the language because the 
compiler can spot a few kinds of mistakes for you, but this looks a 
whole lot more achievable. Surely something like this could be slipped 
into phobos - maybe even into std.thread.

How come this didn't go further? Lack of support for conditions is a 
glaring omission from phobos.



More information about the Digitalmars-d mailing list