Walter is right about transitive readonly - here's the alternative
Steven Schveighoffer
schveiguy at yahoo.com
Thu Sep 13 13:40:19 PDT 2007
"Sean Kelly" wrote
>> My implementation wasn't re-entrant for write locks. I never found it
>> necessary. That said, I know that it is possible to make re-entrant
>> write locks, but probably not by the means that you suggest.
>
> It's possible but to do so I think you'd have to maintain a list of which
> threads held which lock. This doesn't seem terribly efficient, and just
> not worthwhile for something that is of marginal utility.
Since write-lock is exclusive, you only need to know a single thread which
holds the lock. The thread ID can be stored with the lock, so it is easy to
tell/efficient. Basically (in lock() method):
if(locked_id != my_id)
{
lockSysMutex();
locked_id = my_id;
_reentrant++; // initialized to 0
}
and in unlock():
if(--_reentrant == 0)
{
locked_id = 0;
unlockSysMutex();
}
I'd disagree that it is of marginal utility ;) but that's my opinion. I
use it a lot in C# which has reentrant write locks.
-Steve
More information about the Digitalmars-d
mailing list