Walter is right about transitive readonly - here's the alternative

Janice Caron caron800 at googlemail.com
Thu Sep 13 12:35:08 PDT 2007


On 9/13/07, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> > My C++ implementation used an atomic counter for read locks, an atomic
> > counter for (pending) write locks, and an OS mutex.
>
> Using atomic counters, how do you read the write counter, then increment the
> read counter atomically without using the mutex?

You don't have to make the entire process atomic, just the increments
and decrements.

getWriteLock()
{
    atomicIncrement(wcount);
    obtainMutex();
}

releaseWriteLock()
{
    releaseMutex();
    atomicDecrement(wcount);
}

getReadLock()
{
    while (wcount != 0)
    {
        obtainMutex();
        releaseMutex();
    }
    if (atomicIncrement(rcount) == 1)
    {
        obtainMutex();
    }
}

releaseReadLock()
{
    if (atomicDecrement(rcount) == 0)
    {
        releaseMutex();
    }
}

I think that's right. That's from memory. If it's wrong, I can't check
until tomorrow. It looks right to me though.



More information about the Digitalmars-d mailing list