这是一个面试问题.你如何实现读/写互斥?将有多个线程读取和写入资源.我不知道该怎么做.如果需要任何信息,请告诉我.
更新:我不确定我上面的陈述是否有效/可理解.但我真正想知道的是,如何根据互斥锁和其他所需的同步对象在单个对象上实现多次读取和多次写入?
解决方法
查看
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中提到)