c++ - C++中的读写互斥量

标签 c++ multithreading mutex

这是一道面试题。你如何实现读/写互斥体?将有多个线程读取和写入资源。我不知道该怎么做。如果需要任何信息,请告诉我。

更新:我不确定我上面的陈述是否有效/可以理解。但我真正想知道的是如何根据互斥量和其他所需的同步对象实现对单个对象的多次读取和多次写入?

最佳答案

查看 Dekker's algorithm .

Dekker's algorithm is the first known correct solution to the mutual exclusion problem in concurrent programming. The solution is attributed to Dutch mathematician Th. J. Dekker by Edsger W. Dijkstra in his manuscript on cooperating sequential processes. It allows two threads to share a single-use resource without conflict, using only shared memory for communication.

请注意,Dekker 的算法使用 spinlock (不是 busy waiting )技术。
(Th. J. Dekker 的解决方案,E. W. Dijkstra 在他的 EWD1303 paper 中提到) alt text

关于c++ - C++中的读写互斥量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2334364/

相关文章:

c++ - 使用常量长度时 char[] 和 new char[] 之间的区别

go - 如何取消延期结单

c++ - 使用 QMutex::tryLock 和 QMutexLocker

multithreading - 尝试将 AddMessage 批量添加到 Azure 队列

c++ - 如何阻塞线程并恢复它

multithreading - Google Compute Engine 上的 1 个 vCPU 基本上是 1 个物理 CPU 核心的一半吗?

memory - 互斥锁可以代替内存屏障吗

c++ - 使用 std :map 时崩溃

c++ - 点迭代器的奇怪接口(interface)方法

c++ - 为什么这个 TAD 返回 0.0000 而不是相关函数的结果?