By lock helpers I am referring to disposable objects with which locking can be implemented via using
statements. For example, consider a typical usage of the
No, you do not need to do anything special to guarentee that memory barriers are created. This is because almost any mechanism used to get a method executing on another thread produces a release-fence barrier on the calling thread and an aquire-fence barrier on the worker thread (actually they may be full fence barriers). So either QueueUserWorkItem
or Thread.Start
will automatically insert the necessary barriers. Your code is safe.
Also, as a matter of tangential interest Thread.Sleep
also generates a memory barrier. This is interesting because some people naively use Thread.Sleep
to simulate thread interleaving. If this strategy were used to troubleshoot low-lock code then it could very well mask the problem you were trying to find.