In implementation of reader-writer lock, we can make use of the std::shared_mutex
with std::shared_lock
and std::lock_guard
or std::unique_lock
.
Question> Is this new feature writer or reader preferring?
Update based on Andrew's comment
// Multiple threads/readers can read the counter's value at the same time.
unsigned int get() const {
std::shared_lock<std::shared_mutex> lock(mutex_);
return value_;
}
// Only one thread/writer can increment/write the counter's value.
void increment() {
std::unique_lock<std::shared_mutex> lock(mutex_);
value_++;
}
As you can see from above example, I have no control on the reader/writer priority.