Volatile keywords and locking comparisons: When does it use Volatile?
In the multi -threaded environment of C#, Keywords are critical to solve the problem of specific memory synchronization.
Uses and limitations volatile
As explained by Eric Lippert, Keyword indicator compilers and runtime Avoid ordering and optimizing the code for specific variables. In addition, it also indicates that the processor ensures the latest value, even if this needs to suspend other processors to cache it with the main memory.
However, Lippert reminded that Keywords do not guarantee that each processor will suspend its operation and update its cache. Instead, it provides a weaker guarantee for observed memory access. volatile
volatile
When the consistency of memory is required but the performance is also critical, the use of keywords can avoid locking. This happened in:
A variable is shared between multiple threads, but only one thread is written.
The value of the variable is rarely accessed, and the modification rarely occurs. volatile
More resources
More insights on complexity: volatile
volatile
Farewell to volatile
volatile
The above is the detailed content of When Should I Use the Volatile Keyword in C# Instead of Locking?. For more information, please follow other related articles on the PHP Chinese website!