std::shared_mutex with std::shared_lock is reader or writer preferring?

强颜欢笑 提交于 2019-11-29 14:58:13
Howard Hinnant

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.

This answer details how that is accomplished.

In practice:

  • libc++ always uses the mutex+condition variables technique Howard mentioned, not surprisingly.
  • libstdc++ uses pthread_rwlock_t where available, falling back to the algorithm Howard mentioned if it is not. Therefore if pthread_rwlock_t is available, the algorithm used depends on the pthreads implementation. I believe that glibc prefers readers by default.
  • MSVC uses Windows SRWLOCK, whose documentation says

    There is no guarantee about the order in which threads that request ownership will be granted ownership; SRW locks are neither fair nor FIFO.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!