彼得森的锁码取自(德国)维基百科 :
# define FALSE 0 # define TRUE 1 # define N 2 int turn; int interested[N]; void enter_region(int process) { int other; other = 1 - process; interested[process] = TRUE; turn = other; while (interested[other] == TRUE && turn == other) ; } void leave_region(int process) { interested[process] = FALSE; }
有人可以想到一个例子,在leave_region
函数中没有mfence的情况下发生错误吗?
注意:我确信在enter_region
函数中需要一个mfence。
当然。 它不需要任何特别不寻常的情况。
假设在CR中执行计算,最后的操作是将结果存储到存储器中。 进一步假设在CR之后不久,另一个线程读取目标存储器以获得计算结果。 读取不得与写入重新排序,否则将获得错误的值。 为避免这种情况,离开CR时需要mfence
(或其他用作内存屏障的指令)。
以上就是c/c++开发分享我们需要在Peterson锁定x86的unlock()函数中使用mfence吗?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/549454.html