How to Best Use core.sync.rwmutex

Andrew Wiley wiley.andrew.j at gmail.com
Mon Sep 26 00:54:58 PDT 2011


On Mon, Sep 26, 2011 at 12:43 AM, Andrew Wiley <wiley.andrew.j at gmail.com> wrote:
> ReadWriteMutex (http://www.d-programming-language.org/phobos/core_sync_rwmutex.html)
> doesn't seem to play too well with the built-in monitor system. I've
> been thinking about "safe" ways to use it, and the best I've come up
> with is this:
>
> shared class SomeData {
> private:
> ReadWriteMutex rwmutex;
>
> ...
>
> public:
> void readSomeData() {
> synchronized(rwmutex.reader) {
> ...
> }
> }
>
> void writeSomeData() {
> synchronized(rwmutex.writer) {
> ...
> }
> }
>
> }
>
> This seems like it should work, but from TDPL, it doesn't really seem
> to be what shared classes are meant for. Is there a better way to get
> this sort of locking? Was ReadWriteMutex intended to be used
> differently?
>

Actually, I keep trying to come up with a code sample for this, and
I'm not managing to find anything that works and makes any sense
because the shared keyword propagates to the members, so I have to
lock the rwmutex to lock one of its submutexes, and I'm not getting
that to work with the type system at all. Could someone who's done
more with shared/synchronized post a code sample of what this should
look like?


More information about the Digitalmars-d mailing list