gpt4 book ai didi

c++ - 我们是否需要在 x86 上彼得森锁的 unlock() 函数中使用 mfence?

转载 作者:行者123 更新时间:2023-11-28 06:27:18 24 4
gpt4 key购买 nike

Peterson 的锁码取自 (german) wikipedia :

# 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 中进行了一次计算,最后的 Action 是将结果存储到内存中。进一步假设在CR之后不久,另一个线程读取目标内存以获取计算结果。读取不能与写入重新排序,否则将获得错误的值。为避免这种情况,当您离开 CR 时,需要一个 mfence(或其他用作内存屏障的指令)。

关于c++ - 我们是否需要在 x86 上彼得森锁的 unlock() 函数中使用 mfence?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28306064/

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