How does the lock statement ensure intra processor synchronization?

前端 未结 3 1138
萌比男神i
萌比男神i 2021-02-08 04:55

I have a small test application that executes two threads simultaneously. One increments a static long _value, the other one decrements it. I\'ve ensured with

3条回答
  •  甜味超标
    2021-02-08 05:21

    The lock keyword is just syntactic sugar for a pair of System.Threading.Monitor.Enter() and System.Threading.Monitor.Exit() calls. The implementations of Monitor.Enter() and Monitor.Exit() put up a memory fence which entails performing architecture appropriate cache flushing. So your other thread won't proceed until it can see the stores that results from the execution of the locked section.

提交回复
热议问题