Expose underlying Mutex in Condition?

David Nadlinger see at klickverbot.at
Thu Jul 14 11:26:04 PDT 2011


A quick idea I just had, feel free to shoot it down if I missed 
something obvious:

I have been writing some »old-school« multi-threading code lately, and 
it occurred to me that most of the time, I am using condition variables 
like this:

---
auto fooMutex = new Mutex;
auto fooCondition = new Condition(fooMutex);

[…]

// Later in the code:
synchronized (fooMutex) {
   […]
   fooCondition.notifyAll();
   // or
   fooCondition.wait();
}
---

When actually using the condition variable, I often don't care what the 
underlying mutex actually is, I just want to lock it so I can wait() on 
the condition variable or notify() it. Thus, it would be convenient if 
Condition had a »mutex« property exposing the underlying mutex, to avoid 
having to remember which Mutex belongs to which Condition.

What do you think of it? Did I miss something? The »issue« can easily be 
worked around by a suitable naming convention for the pairs of 
Mutexes/Conditions, but still I feel a Condition.mutex property would 
make typical code look cleaner.

David


More information about the Digitalmars-d mailing list