October 06, 2013 Using ReadWriteMutex with synchronized{} ? | ||||
---|---|---|---|---|
| ||||
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? |
October 07, 2013 Re: Using ReadWriteMutex with synchronized{} ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to E.S. Quinn | 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.
|
Copyright © 1999-2021 by the D Language Foundation