If I have a thread that first manipulates a data structure and therefore has a pthread writelock on it, can I let that thread change the lock to a readlocked state without having a race condition that might allow another thread to acquire a writelock at some point during the switch?
Share
Unfortunately, as far as I know, the pthreads standard does not allow for “downgrading” from a writer lock to a reader lock on a
pthread_rwlock_t. Some pthreads implementations might allow extensions that let you transition from holding a writer lock to holding a reader lock without releasing the lock, but this is outside the scope of the SuS / POSIX spec for pthreads. And I don’t believe that the most common case, the Linux/glibc pthreads implementation, allows for this operation.So the short answer to your question is “No.” You’ll need to implement your own reader/writer locks on top of pthread_mutex_t/pthread_cond_t to get the behaviour you want.