我们需要在x86的Peterson的锁的unlock()函数中的一个mfence吗? [英] Do we need an mfence in the unlock() function of Peterson's lock on x86?

查看:183
本文介绍了我们需要在x86的Peterson的锁的unlock()函数中的一个mfence吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Peterson的锁码取自(德国)维基百科

Peterson's lock code taken from (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 功能?

Can somebody think of an example where a bug is happening without an mfence in the leave_region function?

注意:我确定在 enter_region 函数。

推荐答案

它不需要任何特别不寻常的情况。

Sure. It doesn't require any particularly unusual situation.

假设在CR中执行计算,最后一个操作是将结果存储到内存中。进一步假定在CR之后不久,另一个线程为了获得计算结果而读取目标存储器。读取不能与写入重新排序否则将获得错误的值。为了避免这种情况,当你离开CR时需要一个 mfence (或其他作为内存屏障的指令)。

Suppose that a computation is performed in the CR, with the final action being to store the result to memory. Suppose further that soon after the CR, another thread reads the target memory with the purpose of obtaining the computation result. The read must not be reordered with the write else the wrong value will be obtained. To avoid that, an mfence (or other instruction that serves as a memory barrier) is required when you leave the CR.

这篇关于我们需要在x86的Peterson的锁的unlock()函数中的一个mfence吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆