在读写锁的实现中,我们可以使用std::shared_mutexwithstd::shared_lock和std::lock_guardor std::unique_lock。
问题> 这个新功能作者更喜欢还是读者更喜欢?
根据安德鲁的评论更新
参考:
// 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_++;
}
Run Code Online (Sandbox Code Playgroud)
从上面的示例中可以看出,我无法控制读取器/写入器的优先级。