Thread.MemoryBarrier, the last 100 points!

Recently it was Thread.MemoryBarrier crazy
The official explanation for the MSDN:
"In the following way synchronous memory access: the execution of the current thread processor in the reordering of instructions, the first implementation cannot be adopted

Memory access after the call to MemoryBarrier, and then execute memory access before the MemoryBarrier call way"

I will take this explanation as authoritative and reliable.

Code 1:
int init;
int value;
//Thread 1
void SetValue(){
    int value=5;
    Thread.MemoryBarrier();//Init memory write operation, must be in the value memory write
    int init=1;
}
//Thread 2
void GetValue(){
    int _init=init
    Thread.MemoryBarrier()//The value read operation, must be in the init read operation after this
    int _value=value;
}

We can use Thread.VolatileWrite/VolatileRead to replace the above writing, why, we see their internal implementation:
  public static void VolatileWrite(ref int address, int value)
  {
      Thread.MemoryBarrier(); address = value;
  }

  public static int VolatileRead(ref int address)
  {
      int num = address; Thread.MemoryBarrier(); return num;
  }


This code is the code below and 1 equivalent
Code 2:
void SetValue(){
    int value=5;
    Thread.VolatileWrite(ref init);
}
void GetValue(){
    int _init=Thread.VolatileRead(ref init);
    int _value=value;
}


The key here, we look at the MSDN for Thread.VolatileWrite and VolatileRead:

VolatileRead: Read field value. Regardless of the number of processors or processor cache state, the value is the new value written by any processor in a computer.
VolatileWrite: Writes a value to a field immediately, so that the value to all processors in the computer are visible
That is to say we can do the following explanation of code 1:
void GetValue(){,
int _init=init
Thread.MemoryBarrier() //Question: blue logo of this two step can be viewed as an VolatileRead operation, but fuck, see MSDN interpretation of MemoryBarrier, MemoeryBarrier just before and after the operation to ensure order unchanged, but where there is mention every time is to read the new value from memory? In addition to restrict or MemoeryBarrier sequence not disorder, there are other effects, such as the refresh register, register, empty? ? ?
int _value=value
}

Started by Lorin at November 17, 2016 - 11:30 PM

Unknown sleep calendar, LZ seems to know too much

The feeling just stop doing Volatile compiler optimization.,
In order to avoid the read and write the specified variable instruction to optimize away on it.

So this code may be just to the compiler to see:)

Posted by Ellie at November 23, 2016 - 11:54 PM

So sink???? There is nobody.

Posted by Lorin at December 05, 2016 - 12:39 AM

Memory access after the call to MemoryBarrier, and then execute memory access before the MemoryBarrier call way
Cache stack that is calling MemoryBarrier synchronous multiple processors.

Refresh the dirty data cache so it can contain what you say.

Posted by Candice at December 06, 2016 - 12:43 AM

[MethodImpl(MethodImplOptions.NoInlining)]
public static int VolatileRead(ref int address)
{
int num = address;
MemoryBarrier();
return num;
}

Nothing to say. The compiled read code C#.... Personally think that.. MemoryBarrier in addition to not allow out of order, also forced refresh data from memory.. Emphasizes the memory read the order cannot be changed, is it right? It means.. Cannot use the system cache.?

Posted by Elaine at December 21, 2016 - 12:53 AM

What do you want? You want strong women do it

Posted by Humphrey at December 25, 2016 - 1:52 AM