• Uncategorized

About linux : Configure-sharedmutex-to-prefer-writer-over-readers

Question Detail

I have a C++ application compiled with gcc that uses boost::shared_mutex. I use them with shared_lock (reader) and unique_lock (writer).

I am running into writer starvation as explained in Does std::shared_mutex favor writers over readers?.

The linked article says that POSIX threads lib is used and links to another article how PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP can be used with pthreads. There is it described that I should initialize pthread_rwlock_init and set pthread_rwlockattr_setkind_np. However, my pthread rw locks are created by boost, and therefore I cannot pass such custom configuration to it.

Is there a way to globally prefer writers over readers such that boost will pick it up and what would such code look like?

Are there alternatives?

Question Answer

Clone & own the shared_mutex gcc file in your project, the file is small and compiles out of the box and use the alternative implementation that is hidden behind the _GLIBCXX_USE_PTHREAD_RWLOCK_T flag which takes into account writer starvation.
boost::shared_lock and boost::unique_lock work out of the box with such an alternative implementation and the remaining application is not affected.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.