Read write lock is a synchronization technology to solve multi read and write problem. There are three priorities: Read-prefering RW lock, Write-prefering RW lock, and Unspecified priority RW locks.
There are some libraries provide read write lock functions, like:
- POSIX standard WR lock
pthread_rwlock_t
(This WR lock provides different priorities) std::shared_mutex
inC++17
boost::shared_mutex
andboost::upgrade_mutex
inboost library
The read-copy-update (a.k.a. RCU) is another method to solve mutli reader and write problem. Another solution is seqlock
for fewer readers.
A simple persudo implementation:
More details about implementation in C++11
(use atomic variable as spinlock), please see this.
BTH, this simple WR_lock implement method do not gurantee writer has a higher priority.