> -----Original Message-----
> From: Branko Čibej [mailto:[EMAIL PROTECTED]
> Subject: Re: Can a thread write-lock a rwlock that it already has read-
> locked?
> 
> David Barrett wrote:
> 
> >I'm experimenting with the reader/writer locks and have a question:
> >
> >     Can a thread write-lock a thread on which it already holds the read
> >lock?
> >
>
> No. A write lock is always exclusive.

Well yes, I agree.  It's just that a write-lock seems to be a superset of
the read lock.  In my mind, to have the write lock means I also must hold a
read lock, too (because I can both read and write).  Thus from this
perspective, write-locking when I already have the read lock is akin to a
"write lock + nested read-lock".  Nested read locks are allowed (at least I
think they are -- nested mutex locks are, at least), so I think this should
be too.

Anyway, this is all moot.  I understand that APR disallows my desired
behavior (with a reasonable justification), and as a workaround I'm using
mutexes to create a read/write lock that works as I want.

But while I'm on the moot point, I might add I think my desired behavior is
*much* easier to work with.  Consider:

# get read lock
#    result = long operation;
#    if( result ) {
#        get write lock
#           do something
#        release write lock
#    }
# release read lock

Without the ability to write lock while I hold the read lock, I'd need to
write-lock during the "long operation", even though it's not algorithmically
required.  This blocks other threads for an unnecessarily long period.

(If I had an atomic "upgrade to write lock" command I could do that too.
But without that, I can't safely give up the read lock and then grab the
write lock, because a thread might come in and invalidate the results of my
long operation.)

But regardless, I get what APR does, I have a workaround, and all is right
in the world.

-david

Reply via email to