I could not find information about what is the scope at which compiler stop doing instructions reordering when it sees a lock or memory barrier.
For e.g. in below pseudo-code with reference to c++ lang
Is there any difference in ordering of instructions on a,x
in cases where lock
is moved to separate function bar
vs when it is with in function foo
?
Are the answers same if lock
is replaced by a full memory barrier ?
int func foo()
{
read a;
x = 10;
{
lock(mutex);
z++;
unlock(mutex);
}
a += 1;
return x;
}
vs
int func foo()
{
read a;
x = 10;
bar();
a += 1;
return x;
}
func bar()
{
lock(mutex);
z++;
unlock(mutex);
}