Talk about volatile

First, volatile: is a type modifier

effect:

First, to achieve visibility, that a thread changes the value of a variable, which is a new value for the other thread is immediately visible. (Achieving visibility)

// Thread 1

boolean stop = false;

while(!stop){

    doSomething();

}

// Thread 2

Each thread of stop has its own working memory during the running process, then when thread 1 is running, it will copy the value of the stop variable in its working memory.

  Then when thread 2 changes the value of the stop variable, but has not had time to write it into main memory, thread 2 turns to do other things, then thread 1 will continue to loop because it does not know the change of thread 2 to the stop variable. Go on.

  But after modified with volatile, it becomes different: (Forced writing, the original is invalid, and it will be read again after invalidation)

  First: using the volatile keyword will force the modified value to be written to main memory immediately;

  Second: If the volatile keyword is used , when the thread 2 is modified, it will cause the cache line of the cache variable stop in the working memory of the thread 1 to be invalid. invalid);

Third: Since the cache line of the cache variable stop in the working memory of thread 1 is invalid, thread 1 will go to main memory to read the value of the variable stop again.

The general steps are to modify and store it in the main memory, the original is invalid, so it will be read again, and the latest value will be read when it is read again

  Then when thread 2 modifies the stop value (of course, there are 2 operations, modify the value in thread 2's working memory, and then write the modified value to memory), it will cause the cache line of the variable stop in the working memory of thread 1 Invalid, then when thread 1 reads, it finds that its cache line is invalid, it will wait for the main memory address corresponding to the cache line to be updated, and then go to the corresponding main memory to read the latest value.

  Then thread 1 reads the latest correct value. = true;

The second is to prohibit instruction reordering. (Achieve order)

The volatile keyword prohibits instruction reordering in two ways:

  1) When the program performs a read operation or a write operation to a volatile variable, all changes to the operation in front of it must have been performed, and the result has been visible to the subsequent operation; the operation behind it must have not been performed;

  2) During instruction optimization can not be volatile variable in front on its rear executed statements , nor can the latter into its volatile variables statements executed on the front.

  Perhaps the above is more circumscribed, and a simple example:

// x and y are non-volatile variables

// flag is a volatile variable

x = 2; // Statement 1

y = 0; // Statement 2

flag = true; // Statement 3

x = 4; // Statement 4

y = -1; // Statement 5

   Since the flag variable is a volatile variable, when the instruction reordering process is performed, Statement 3 will not be placed in front of Statement 1 and Statement 2, nor will Statement 3 be placed after Statement 4 or Statement 5. However, it should be noted that the order of statement 1 and statement 2, and the order of statement 4 and statement 5 are not guaranteed.

  And the volatile keyword can guarantee that when statement 3 is executed , statement 1 and statement 2 must be executed, and the execution results of statement 1 and statement 2 are visible to statement 3, statement 4, and statement 5.

Visibility volatile memory variables and order is a memory barrier (Based on Memory Barrier) achieved . The memory barrier, also known as the memory fence, is a CPU instruction.

Three functions of the memory barrier: 1) It ensures that when the instruction is reordered, it will not arrange the instructions behind it to the position before the memory barrier, nor will it arrange the previous instruction behind the memory barrier and execute the sentence to the memory barrier At the time of the instruction, all operations before it have been completed; (orderly)

  2) It will force the modification operation to the cache to be written to main memory immediately; (Visibility)

  3) If it is a change operation, it will invalidate the corresponding cache line in other threads. (Visibility)

Guess you like

Origin www.cnblogs.com/wl889490/p/12688014.html
Recommended