I read the article Synchronization and Multiprocessor Problems , and I have a question about InterlockedCompareExchange and InterlockedExchange. The question is about the last example in the article. They have two variables, iValue and fValueHasBeenComputed , and in CacheComputedValue() they modify each of them using InterlockedExchange :
InterlockedExchange ((LONG*)&iValue, (LONG)ComputeValue());
I understand that I can use InterlockedExchange to modify iValue , but is it enough to do this
iValue = ComputeValue();
So, do I really need to use InterlockedExchange to install iValue? Or other threads will see iValue correctly, even if iValue = ComputeValue(); . I mean, other threads will see iValue correctly, because there is InterlockedExchange after it.
There is also an article on the Principle of Serial Memory Model for Microsoft Enterprise Code Platforms . There is an example 3.1.1 with more or less the same code. One of the recommendations of Make y interlocked . Please note - not both y and x .
Update
Just to clarify the issue. The problem is that I see a contradiction. In the example from Synchronization and Multiprocessing Issues, two InterlockedExchange . In contrast, in Example 3.1.1, the βMain Changeβ (which, it seems to me, is very similar to the first example) Herb Sutter gives this recommendation
"Make y blocked: if y is blocked, then there is no race on y because it is atomically updatable, and there is no race on x because β b β d."
. In this project, Herb does not use two interrelated variables (if I'm right, it uses InterlockedExchange only for y ).
c ++ windows atomic winapi interlocked
Sergei Kurenkov
source share