Walter is right about transitive readonly - here's the alternative
Steven Schveighoffer
schveiguy at yahoo.com
Thu Sep 13 13:21:28 PDT 2007
"Janice Caron" wrote
> getReadLock()
> {
> while (wcount != 0)
> {
> obtainMutex();
> releaseMutex();
> }
> if (atomicIncrement(rcount) == 1)
> {
> obtainMutex();
> }
> }
I understand that this might not be exactly what you have, but I do see a
chance for a problem here.
Thread 1 and 2 are attempting to read the value, and nobody is writing it.
Both threads skip the while loop, but then switch out of context to Thread 3
which write-locks the data. Thread 1 does the first increment, tries to
lock the mutex, gets put to sleep. Thread 2 switches back into context,
increments the read counter, then proceeds, however thread 3 can write while
thread 2 is reading.
Not a very likely situation, but of course those types are the worst when it
comes to multithreaded programming. Of course, you may have already solved
this problem :) but I can't see how you can do it without making the "read
write counter, if 0 increment read counter, if that is now 1, lock" atomic.
-Steve
More information about the Digitalmars-d
mailing list