14

do you have any info about the subject? any problem with boost::shared_mutex in particular and with reader-writer mutexes at all?

reader-writer mutex can be misused, e.g. frequent writer locks reduces performance, even in comparison with simple mutex. but there're a lot of cases when many readers often require a shared resource that can be modified by writer really infrequently.

Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112
  • 8
    It is accepted in C++14. See http://en.cppreference.com/w/cpp/thread/shared_mutex or http://en.wikipedia.org/wiki/C++14 – Tom Moers May 30 '13 at 07:58
  • 1
    The link in the comment above should be http://en.cppreference.com/w/cpp/thread/shared_timed_mutex (`std::shared_mutex` is a simpler type, which is not part of C++14 but will be in C++17). – Jonathan Wakely Mar 21 '16 at 15:27

2 Answers2

12

Anthony Williams is an influential member of the C++ standards committee. He co-authored many of the proposals that led to the inclusion of the thread library in the C++11 Standard. You can read his objections in this commentary. Nevertheless, it did make it into C++17.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    N.B. the equivalent of `boost::shared_mutex` made it into C++14, but renamed to `shared_timed_mutex`. C++17 will also add `shared_mutex` which is the same but without the timed waiting functions (analogous to `mutex`/`timed_mutex` and `recursive_mutex`/`recursive_timed_mutex`). – Jonathan Wakely Mar 21 '16 at 15:26
10

As told in the mail referenced by Hans, the problem with shared_mutex is its high overhead. Therefore shared_mutex gives only a benefit when resources are held for a long time and only few threads are competing for the resources, which is very rare.

I found another detailed article about the issue.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
Enno Gröper
  • 4,391
  • 1
  • 27
  • 33