Answer by Marc Gravell for Do I need to lock or mark as volatile when...
Firstly, threading is tricky ;-pYes, despite all the rumours to the contrary, it is required to either use lockorvolatile (but not both) when accessing a bool from multiple threads.For simple types and...
View ArticleAnswer by Daniel Brückner for Do I need to lock or mark as volatile when...
Locking is not required because you have a single writer scenario and a boolean field is a simple structure with no risk of corrupting the state (while it was possible to get a boolean value that is...
View ArticleAnswer by hughdbrown for Do I need to lock or mark as volatile when accessing...
Look up Interlocked.Exchange(). It does a very fast copy into a local variable which can be used for comparison. It is faster than lock().
View ArticleAnswer by Adam Robinson for Do I need to lock or mark as volatile when...
For thread synchronization, it's recommended that you use one of the EventWaitHandle classes, such as ManualResetEvent. While it's marginally simpler to employ a simple boolean flag as you do here (and...
View ArticleAnswer by bruno conde for Do I need to lock or mark as volatile when...
_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...
View ArticleDo I need to lock or mark as volatile when accessing a simple boolean flag in...
Lets just say you have a simple operation that runs on a background thread. You want to provide a way to cancel this operation so you create a boolean flag that you set to true from the click event...
View Article