Do we need to use the Peterson lock unlock () function on x86?

Peterson 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; } 

Can someone think of an example where an error occurs without the function of the leave_region function?

NB: I know for sure that the mfence function is required in the enter_region function.

0
source share
1 answer

Of course. This does not require a particularly unusual situation.

Suppose that the calculation is performed in CR, and the final action is to store the result in memory. Suppose further that shortly after CR another thread reads the target memory in order to obtain the result of the calculation. The reading should not be reordered using the else record, and the wrong value will be received. To avoid this, exiting CR requires mfence (or another instruction that serves as a memory barrier).

+1
source

Source: https://habr.com/ru/post/921006/


All Articles