www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using ReadWriteMutex with synchronized{} ?

reply "E.S. Quinn" <anonymous example.com> writes:
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?
Oct 06 2013
parent =?UTF-8?B?U8O2bmtlIEx1ZHdpZw==?= <sludwig outerproduct.org> writes:
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.
Oct 07 2013