Thread safe usage of lock helpers (concerning memory barriers)

后端 未结 1 1579
我在风中等你
我在风中等你 2021-01-13 01:14

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

相关标签:
1条回答
  • 2021-01-13 01:43

    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.

    0 讨论(0)
提交回复
热议问题