Roaming sharing and memory visibility

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()); // don't understand InterlockedExchange ((LONG*)&fValueHasBeenComputed, TRUE); // understand 

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 ).

+8
c ++ windows atomic winapi interlocked
source share
4 answers

They did this to prevent partial read / write if the iValue address iValue not consistent with the address that guarantees atom access. this problem would occur when two or more physical threads try to write a value at the same time or one reads and one tries to write at the same time.

As a secondary point, it should be noted that storages are not always displayed globally, they will only be displayed during serialization, whether it is a fence or a bus lock.

+1
source share

You just get an atomic operation with InterlockedExchange . Why do you need this? Reason InterlockedExchange does 2 things.

  • Replaces the value of a variable
  • Returns the old value

If you do the same in two operations (thus, check the value first and then replace), you can get a screwdriver if between the two (2)

And you also prevent data races by this value. here you get a good explanation why reading / writing on LONG is not atomic

0
source share

There are two plausible solutions to the contradiction that you have observed.

Firstly, the second document is simply erroneous in this regard. This is, after all, a project. I note that the specific example in which you are referencing specifically states that the programmer cannot rely on the fact that the records are atomic, which means that both records must really be locked.

Another is that in this particular example, an optional additional lock may not be necessary, because this is a very special case: only one bit of the variable is changed. However, the specification being developed does not mention this as a room, so I doubt that it is intentional.

0
source share

I think that in this discussion there is an answer to the question: Implicit memory barriers .

Question : Does InterlockedExchange (implicit full fence) call on T1 and T2, gurentess, that T2 will "see" the record made by T1, before the fence? (Variables A, B, and C), although these variables are not on the same cache line as Foo and Bar?

Answer : Yes - the full fence generated by InterlockedExchange, it will be that the records in A, B and C are not reordered for the fence implicit in the InterlockedExchange call. This is the semantics point of the memory barrier. They should not be in the same line cache.

Memory Locks: Hardware for Software Hackers and Problems with programming locks for the Xbox 360 and Microsoft Windows are also on the lookout.

0
source share

All Articles