TDPL, shared data, and Phobos

Sean Kelly sean at invisibleduck.org
Sun Jul 18 16:37:16 PDT 2010


Graham St Jack Wrote:
> 
> The code I am trying to write is a simple synchronized class with a 
> Condition, but I can't create a Condition on a shared "this".
> 
> A cut-down version of what I want to write is:
> 
> synchronized class Foo {
>   Condition mCondition;
>   this() {
>     mCondition = cast(shared) new Condition(this);
>   }
>   void some_method() {
>   }
> }
> 
> I realise that Condition wants a Mutex, but a synchronized class already 
> has an implicit one which is implicitly used by all the methods, so the 
> above is definitely what I want to write.

The built-in mutex is created on first use, so what you can do is drop in core.sync.mutex.Mutex before this happens (the library Mutex can be assigned as an object monitor).  If 'shared' weren't an issue, the code would be:

synchronized class Foo {
    Condition mCond;
    Mutex      mLock;
    this() {
        mLock = new Mutex(this); // make mutex this object's monitor
        mCond = new Condition(mLock); // bind condition to mLock
    }

    void some_method() { // locks mLock
        mLock.notify(); // safe because mLock is locked
    }
}



More information about the Digitalmars-d mailing list