gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:31 24 4
gpt4 key购买 nike

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

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

最佳答案

查看 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com