看下组织结构:
System.Object
System.MarshalByRefObject
System.Threading.WaitHandle
System.Threading.Mutex
System.Threading.Semaphore
System.Threading.EventWaitHandle
System.Threading.ManualResetEvent
System.Threading.AutoResetEvent
System.Object
System.Threading.Interlocked
System.Threading.Monitor
System.Threading.ReaderWriterLock
1, lock 关键字其实就是对 Monitor 类的 Enter()和 Exit()方法的封装。通过 try......catch......finally 语句块确保在 lock 语句块结束后执行 Monitor.Exit()方法,释放互斥锁。下面2段代码等价:
lock(locker) { //do something }