Tools: Rarely Unique Shared Mutexes

Tools: Rarely Unique Shared Mutexes

Source: Dev.to

An ru_shared_mutex may provide better performance than an instance of std::shared_mutex. The performance advantage is proportional to the following: A big drawback is that each rarely unique shared mutex must be the singleton of a distinct class, specifically, an instantiation of the ru_shared_mutex::c class template. Each instantiation has the same interface as std::shared_mutex for shared and exclusive locking. Waiting unique locks always take priority over waiting shared locks. Multiple threads waiting for a unique lock maynot get the lock in the order that they called 'lock()'. Requires C++17 or later. https://github.com/wkaras/C-plus-plus-intrusive-container-templates ru_shared_mutex.h is the header, ru_shared_mutex.cpp is the implementation file. Logic and speed tests are in test_ru_shared_mutex*.cpp. I ran one of the tests (test_ru_shared_mutex_speed2.cpp), on an x86 desktop with 4 physical / 8 logical CPU cores. The test simulates an data structure accessed from multiple threads that never blocks on reads by having two redundant copies, each with its own shared mutex. When there was one write per 999 reads, the number of reads using ru_shared_mutex was more than 17 times the number of reads using std::shared_mutex. When there as one write per 19,999 reads, the number reads using ru_shared_mutex was more than 32 time the number of reads using std::shared_mutex. If anyone has easy access to a server with hundreds of CPU cores, it would be great if you could run test_ru_shared_mutex_speed2.cpp (with -DNDEBUG and at least -O2), and post the output. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse - The ratio of shared locks to unique locks.
- The number of CPU processor cores.
- The threads that take the lock are static versus dynamic.