1 internal sealed class SimpleWaitLock : IDisposable 2 { 3 //(信号量)允许多个线程并发访问一个资源 4 //如果所有线程以只读方式访问资源则是安全的 5 private Semaphore m_Semaphore; 6 7 public SimpleWaitLock(int maxConcurrent) 8 { 9 m_Semaphore = new Semaphore(maxConcurrent, maxConcurrent); 10 } 11 12 public void Enter() 13 { 14 //在内核中阻塞,直到资源可用 15 m_Semaphore.WaitOne(); 16 } 17 18 public void Leave() 19 { 20 //让其它线程使用资源 21 m_Semaphore.Release(); 22 } 23 24 public void Dispose() 25 { 26 m_Semaphore.Dispose(); 27 } 28 }