Home >Backend Development >C++ >Should I Use the Volatile Keyword in C# Multithreading, or Are Locks Always Better?
C# multi -thread programming: Use Volatile keywords with caution, the lock mechanism is more reliable
In C# multi -thread programming,
Keywords are often misunderstood and abused. This article will explore the application of and explain why it should usually be avoided.
volatile
volatile
Senior C# expert Eric Lippert pointed out that not only prevent the compiler from optimizing, but also instructs the processor to ensure the latest value, even if this will suspend other processors. However, this understanding is not complete.
volatile
methods. Why avoid using the Volatile field?
Lippert is strongly recommended to avoid using the volatile
field. They show that the multi -threaded problem is trying to deal with it without appropriate synchronization. The lock mechanism provides stronger memory consistency and the sequence of access. Interlocked
field. More resources
volatile
For more information, please refer to the following articles:
volatile
The impact of understanding low -locking technology on multi -threaded applications
The alternative of the keyword of the volatile keyword
The above is the detailed content of Should I Use the Volatile Keyword in C# Multithreading, or Are Locks Always Better?. For more information, please follow other related articles on the PHP Chinese website!