volatile to ensure the orderliness of it?

to ensure orderly volatile
multi-threaded body of code to execute their preemption time slice, we will feel a thread is executing the same time, in addition to the introduction of a time slot other than due to the optimization processor instructions and rearrangement, the CPU further possible to enter the code out of order, for example, we get the data to write to the library, search, delete these three operations, which will likely to be involved in ordering problem.

volatile program can prohibit instruction reordering, which ensures that the code will be strictly enforced in the order code. This ensures orderly.
Operation is modified volatile variables, strictly according to the order of execution of code, the code that is executed when the volatile variables which must finished preceding code, the code behind must not executed.

volatile keyword prohibition instruction reordering has two meanings:
  1) When the program executes a read operation or write operation volatile variables, change in front of the entire operation has been affirmative, and the result is already visible on the back of the operation; at behind operation certainly not performed;

2) During instruction optimization can not be performed in place behind the statement of access to volatile variables, nor can the volatile variables back into the front statement execution.

Published 59 original articles · won praise 15 · views 556

Guess you like

Origin blog.csdn.net/qq_45287265/article/details/105055204
Recommended