views:

198

answers:

1

Hi,

I have some questions regarding read write locks in POSIX Pthreads on a *nix system, say Linux for example.

I wish to know what is the default bias for read write lock i.e does it prefer reads over writes or vice versa ? Does it provide some api to change this default behaviour.

Does posix pthread provide some api so that we could change the pthread_rwlock_t to prevent writer starvation ? From what i have read(please correct me if i am wrong), the default implementation is biased towards reader threads and so writer threads can face starvation.

I have read the sample implementation of rw lock from the book Programming with Posix threads by David Butenhof.

I wish to know how posix pthreads handle starvation of writer threads ? Is there some api using which we could set the attributes of the read write lock that would prevent write starvation(i have never heard about that) ? Or does the user have to handle this problem ?

If you think that the answer is implementation defined then please give me example of how its done in Linux, because thats what i am looking for.

Please note that i just want solutions for a *nix system. Don't think that i am rude, but posting some windows specific code is useless for me.

Thank you all for your help and patience :)

+5  A: 

This does indeed depend on the implementation - so since you have asked about Linux specifically, my comments are refer to the current NPTL implementation of pthreads, which is used in modern glibc.

There are two related, but separate, issues here. Firstly, there is this situation:

  • There are read locks currently held, and writers waiting. A new thread tries to take a read lock.

The default action here is to allow the reader to proceed - effectively "jumping the queue" over the writer. You can, however, override this. If you use the pthread_rwlockattr_setkind_np() function to set the PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP flag on the attr that you pass to pthread_rwlock_init(), then your rwlock will block the reader in the above situation.

The second situation is:

  • The last holder releases the lock, and there are both readers and writers waiting.

In this situation, NPTL will always wake up a writer in preference to a reader.

Taken together, the above means that if you use the PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP flag, your writers shouldn't be starved (of course, now a continuous stream of writers can starve the readers. C'est la vie). You can confirm all this by checking the sources (it's all very readable) in pthread_rwlock_rdlock.c and pthread_rwlock_unlock.c.

Note that there is also a PTHREAD_RWLOCK_PREFER_WRITER_NP, but it appears not to have the right effect - quite possibly a bug.

caf
thanks a lot. This is what i was looking for. One more thing, is the function pthread_rwlockattr_setkind_np() a POSIX api or only linux specific ? I could not see it in the /usr/include/pthread.h header file on my linux system(may be its old). And what does "np" stands for ? Thanks a lot :)
ghayalcoder
It's listed as a UNIX98 extension here: http://nptl.bullopensource.org/Tests/Optimization-level-in-nptl.html but I believe that's an error, and it's actually a GNU extension (as are all the other functions ending in `_np`). Certainly in practice it's not available elsewhere. I presume `_np` is short for `NPTL`, which means "Native POSIX Threads Library". I'm not sure exactly which version of glibc introduced it, but glibc 2.7 on my box has it. Maybe ask on the glibc mailing list?
caf
Thanks a lot caf :)
ghayalcoder