Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Obj syncing like Java

Moderators: larsivi kris

Posted: 09/06/08 10:35:09

I want to use a class to synchronize on it and call wait() notifyAll() in exactly the same way as in Java? Is it possiblle to make the class provide this behaviour?

An internal Mutex/Condition pair can be used to get the wait()/notifyAll() methods.

But how can it be made, that synchronizing on the object locks in fact the mutex? Can i set the mutex as the Monitor of class A?

// original
class A : B {
}

// use of A should not need changes
A a = ...;
synchronize(a){
  a.notifyAll();
}

// impl for notifyAll, but how make synchronize lock the mutex?
class A : B {
  Mutex m; Condition c;
  this(){
    m = new Mutex;
    c = new Condition(m);
  }
  void notifyAll(){
    c.notifyAll();
  }
}

There are no responses to display.