在讀寫器鎖定的實現中,我們可以使用std::shared_mutex
並使用std::shared_lock
和std::lock_guard
或std::unique_lock
。std :: shared_mutex與std :: shared_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_++;
}
正如你可以從上面的例子看,我對讀/寫器的優先級沒有控制權。
如果你實現自己的讀/寫鎖,是不是取決於你如何實現它? –
@AndrewHenle請檢查我的更新問題。 – q0987