0

I am implementing a C++ multithreading app and I have a lot many readers than writers (ratio 30:1 at least) on some variables and I thought of using RWLocks(maybe pthread_rwlock) instead of Mutexes(std::mutex), because they seem much suitable for many readers.

What I read:
https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock
https://www.arangodb.com/2015/02/comparing-atomic-mutex-rwlocks/
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/locks/ReadWriteLock.html (java but I think that the principles are the same)

But it seems that custom implementations of RWLock contains internally at least a mutex, so why use RWLock?

Taw
  • 479
  • 3
  • 15
  • 1
    See also `std::shared_mutex`: https://en.cppreference.com/w/cpp/thread/shared_mutex – Richard Critten Dec 09 '19 at 16:21
  • 1
    rwlock is useful when concurrent readers perform 'long' operations with the resoure. Using rwlock for instance just to read/update value in hash map is usually indeed slower because rwlock is more complex by itself. if your goal is performance you should probably measure your scenarios with different aproaches and select the solution accordingly. – dewaffled Dec 09 '19 at 16:24
  • I am using C++11 unfortunately. @dewaffled - thanks, indeed that's what the java documentation says also. For a caching mechanism (my situation), I think the RWlock is an overkill because I am just reading some std::unordered map/std::map... – Taw Dec 09 '19 at 17:54

0 Answers0