pthreads: reader/writer locks, upgrading read lock to write lock
Posted
by
ScaryAardvark
on Stack Overflow
See other posts from Stack Overflow
or by ScaryAardvark
Published on 2010-03-09T08:28:42Z
Indexed on
2012/12/07
5:04 UTC
Read the original article
Hit count: 275
I'm using read/write locks on Linux and I've found that trying to upgrade a read locked object to a write lock deadlocks.
i.e.
// acquire the read lock in thread 1.
pthread_rwlock_rdlock( &lock );
// make a decision to upgrade the lock in threads 1.
pthread_rwlock_wrlock( &lock ); // this deadlocks as already hold read lock.
I've read the man page and it's quite specific.
The calling thread may deadlock if at the time the call is made it holds the read-write lock (whether a read or write lock).
What is the best way to upgrade a read lock to a write lock in these circumstances.. I don't want to introduce a race on the variable I'm protecting.
Presumably I can create another mutex to encompass the releasing of the read lock and the acquiring of the write lock but then I don't really see the use of read/write locks. I might as well simply use a normal mutex.
Thx
© Stack Overflow or respective owner