apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Branko ─îibej <br...@xbc.nu>
Subject Re: Can a thread write-lock a rwlock that it already has read-locked?
Date Sun, 17 Oct 2004 19:54:18 GMT
David Barrett wrote:

>Anyway, that's my goal, and while I'd love any feedback on my approach
>(especially to point out any gaping logic holes I've overlooked), I think
>I'm all set.
I think your algorithm is a deadlock waiting to happen. Consider the 
following situation:

    thraad A acquires read lock
    thread B acquires read lock
    thread C acquires read lock
    thread A requests write lock (blocks)
    thread B requests write lock (blocks)
    thread C releases read lock

At this point, threads A and B are both waiting for other readers to 
release their locks, and there are no writers yet. So they're waiting 
for each other. Even if you allow a single thread to own a write and any 
number of read locks simultaneously, you can't avoid this kind of 
deadlock unless each thread releases all its read locks before acquiring 
a write lock. The best you can do is to fail a write-lock request if 
another thread is waiting for the write lock, but to avoid the deadlock 
the thread whose write-lock failed would still have to release all its 
read locks before retrying the request.

Again, you end up with the algorithm I showed in my previous post, 
except that the code gets even more complex.

All in all, I'd advise not trying to invent a "better" way of locking, 
but to use proven methods. Locking is notoriously tricky.

-- Brane

View raw message