简体   繁体   English

带有std :: shared_lock的std :: shared_mutex是读者还是作家更喜欢?

[英]std::shared_mutex with std::shared_lock is reader or writer preferring?

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 . 在实现读写器锁定时,我们可以将std::shared_mutexstd::shared_lockstd::lock_guardstd::unique_lock

Question > Is this new feature writer or reader preferring? 问题 >这个新功能作家还是读者更喜欢?

Update based on Andrew's comment 根据安德鲁的评论进行更新

Reference : 参考

  // 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. 从上面的示例中可以看到,我无法控制读写器的优先级。

In practice: 在实践中:

  • libc++ always uses the mutex+condition variables technique Howard mentioned, not surprisingly. 毫不奇怪,libc ++始终使用霍华德提到的互斥体+条件变量技术。
  • libstdc++ uses pthread_rwlock_t where available, falling back to the algorithm Howard mentioned if it is not. libstdc ++在可用的地方使用pthread_rwlock_t ,如果没有的话,可以回溯到霍华德提到的算法。 Therefore if pthread_rwlock_t is available, the algorithm used depends on the pthreads implementation. 因此,如果pthread_rwlock_t可用,则使用的算法取决于pthreads实现。 I believe that glibc prefers readers by default. 我相信glibc默认情况下更喜欢读者。
  • MSVC uses Windows SRWLOCK , whose documentation says MSVC使用Windows SRWLOCK ,其文档

    There is no guarantee about the order in which threads that request ownership will be granted ownership; 无法保证请求所有权的线程被授予所有权的顺序。 SRW locks are neither fair nor FIFO. SRW锁既不是公平的也不是FIFO。

It is neither (if implemented properly). 两者都不是(如果实施正确)。 Instead readers and writers are chosen to be next by a fair technique. 取而代之的是,读者和作家被公平的技术选为下一个。 And that is the reason that this characteristic is neither settable in the API, nor specified. 这就是无法在API中设置或指定该特性的原因。

This answer details how that is accomplished. 该答案详细说明了如何实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:使用ReaderLock = std :: shared_lock的&#39;shared_mutex&#39;不是&#39;std&#39;的成员 <std::shared_mutex> ; - error: ‘shared_mutex’ is not a member of ‘std’ using ReaderLock = std::shared_lock<std::shared_mutex>; 我可以在 std::shared_mutex 上使用 std::shared_lock 更改数据吗? - Can I change data with a std::shared_lock on a std::shared_mutex? std :: lock()是否等同于boost :: shared_mutex? - std::lock() equivalent for boost::shared_mutex? std::shared_mutex 不与执行 lock_shared() 的线程一起缩放 - std::shared_mutex not scaling with threads doing lock_shared() 错误:没有匹配函数调用&#39;boost :: shared_lock <boost::shared_mutex> :: shared_lock(const Lock&)&#39; - error: no matching function for call to 'boost::shared_lock<boost::shared_mutex>::shared_lock(const Lock&)' 无法将参数1从&#39;const boost :: shared_mutex&#39;转换为&#39;const boost :: shared_lock <Mutex> &” - cannot convert parameter 1 from 'const boost::shared_mutex' to 'const boost::shared_lock<Mutex> &' 用 std::atomic 实现的 shared_lock - shared_lock implemented with std::atomic std :: mutex和std :: shared_mutex之间的区别 - difference between std::mutex and std::shared_mutex 尝试std :: lock [_unique] std :: shared_mutex的线程是否可以被调用std :: lock_shared的线程饿死? - Can thread trying to std::lock[_unique] an std::shared_mutex be starved by threads calling std::lock_shared? 为什么要使用std :: mutex而不是boost :: shared_mutex? - Why ever use std::mutex instead of boost::shared_mutex?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM