Using ReadWriteMutex with synchronized{} ?
Sönke Ludwig
sludwig at outerproduct.org
Mon Oct 7 10:08:50 PDT 2013
Am 06.10.2013 23:25, schrieb E.S. Quinn:
> I need to share an associative array between two threads, and to that
> extent I'd like to make the whole thing synchronized. And I'd like to
> use the built-in synchronized{} blocks, and I'd also like to use the
> ReadWriteMutex from core.sync.rwmutex, since it seems pretty much
> tailor-made for this sort of thing.
>
> Is it possible to, for example, tell D that I want the enclosing class
> synchronized with a ReadWriteMutex, and then specifcy which functions
> need a reader lock and which need writer locks? Or will I have to lock
> and unlock the mutex manually?
Using "synchronized" should work using the reader/writer properties:
---
auto rwmutex = new ReadWriteMutex;
synchronized (rwmutex.reader) {
// do something that reads from the protected memory area
}
synchronized (rwmutex.writer) {
// do something that reads/writes from/to the protected memory area
}
---
You'll have to synchronize inside of class methods according to
read/write though, AFAIK. "synchronized class" works only for the simple
case of all public methods being synchronized the same way.
More information about the Digitalmars-d-learn
mailing list