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
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.