【发布时间】:2017-04-09 16:50:41
【问题描述】:
在读写锁的实现中,我们可以使用std::shared_mutex与std::shared_lock和std::lock_guard或std::unique_lock。
问题>是这个新功能的作者还是读者更喜欢?
根据 Andrew 的评论更新
// 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_++;
}
从上面的例子可以看出,我无法控制读写器的优先级。
【问题讨论】:
-
如果你实现自己的读/写锁,那不取决于你是如何实现的吗?
-
@AndrewHenle 请检查我的更新问题。