In the application scenario of
keywords volatile
In C#,
volatile
According to Eric Lippert, the "
volatile
However, The guarantee provided by read and write is weaker than synchronous primitives such as locks. They actually do not guarantee that all processors stop updating the cache, but only guarantee the order of memory access before and after reading and writing. Therefore, Lippert strongly recommends not to use the
volatile
On the contrary, the lock should be used to ensure consistent memory reading and modification, and ensure that there is only one thread to access a specific memory location at a time. Under normal circumstances, the lock -up field is more desirable, unless the use of lock is a very specific scene of major performance problems. volatile
volatile
The above is the detailed content of When Should I Use the `volatile` Keyword in C# and When Should I Use Locks Instead?. For more information, please follow other related articles on the PHP Chinese website!