Quantcast
Viewing all articles
Browse latest Browse all 6

Answer by bruno conde for Do I need to lock or mark as volatile when accessing a simple boolean flag in C#?

_cancelled must be volatile. (if you don't choose to lock)

If one thread changes the value of _cancelled, other threads might not see the updated result.

Also, I think the read/write operations of _cancelled are atomic:

Section 12.6.6 of the CLI spec states:"A conforming CLI shall guarantee that read and write access to properly aligned memory locations no larger than the native word size is atomic when all the write accesses to a location are the same size."


Viewing all articles
Browse latest Browse all 6

Trending Articles