0

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?

Lemon Sky
  • 677
  • 4
  • 10

1 Answers1

0

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.

Lemon Sky
  • 677
  • 4
  • 10