linux - How to prevent writer starvation in a read write lock in pthreads -
I have some questions about reading POSIX pthreads on a * nix system-write locks, for example, says Linux is.
I want to know what is the default bias for the lock to read, likes to write it or vice versa? Does it provide some APIs to replace this default behavior?
Does the posix pthread provide some APIs so that we can replace pthread_rwlock_t to stop the author's hunger? What I have read (if I am wrong, correct me), the default implementation is biased towards reader threads and therefore the author threads can face starvation.
I have read sample implementations of RW Lock from this book, programming with Pozzys Threads by David Boothhoff.
I want to know how to handle the ground water of author Thread? Are some APIs used that can be used to set the properties of the lock locks which prevent them from writing starvation (I have never heard of it)? Or does the user handle this problem?
If you think the answer is implementation-defining, please give me an example of how it is in Linux, because what I am looking for.
Please note that I only want a * nix system solution Do not think that I am rude, but posting a few windows-specific codes is useless for me.
Thanks for all your help and patience :)
it really depends on implementation - so when you ask specifically about Linux, then refer to current Anpitiel implementation of my comments pthreads, which is used in modern Glibik.
Here are two related, but different, here are the problems. First of all, this is the situation:
- There are current locally held locks, and the authors are waiting. A new thread tries to take a reading lock.
The default action here allows the reader to proceed - effectively "climbing the queue" on the author. However, you can override it. If you use the are pthread_rwlockattr_setkind_np ()
function to install PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
flag attr
that you have pthread_rwlock_init () < / Code>, your call will block the reader in the above position.
The second position is:
- The last holder issues a lock, and both readers and authors are waiting.
In this situation, NPTL will always wake the reader to a writer in priority.
taken together, the above means that if you PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
flag, its authors should not be hungry (of course, now hungry for a steady stream of readers of the authors compete c'est ). All of you can confirm this by checking the sources in you (this is all very readable).
Note that there is also a PTHREAD_RWLOCK_PREFER_WRITER_NP
, but it looks like not to the right impression - quite possibly a bug ( or possibly No - see ).
Comments
Post a Comment