当前位置:网站首页>C: use of mutex

C: use of mutex

2022-06-24 07:02:00 Machine vision 001

1. The mutex lock

 Definition :
private static readonly object m_objLock = new object();
 
 Use :
lock (m_objLock)
{
  // todo
}
 effect : Will lock the contents of the code block , And prevent other threads from entering the code block , Until the code block is completed , Release the lock .

Be careful : The defined lock object should be : private + static state + read-only + Object of reference type , This prevents external changes to the lock object .

2. The mutex Monitor

 Definition :
private static readonly object m_objLock = new object();
 Use :
Monitor.Enter(m_objLock);
//todo
Monitor.Exit(m_objLock);
 effect : Will lock the contents of the code block , And prevent other threads from entering the code block , Until the code block is completed , Release the lock .

Be careful : Be careful : The defined lock object should be : private + static state + read-only + Object of reference type , This prevents external changes to the lock object .

Monitor Yes TryEnter The function of , It can prevent deadlock problems ,lock No, .

3. The mutex Mutex

 Definition :
private static readonly Mutex mutex = new Mutex();
 Use :
mutex.WaitOne();
//todo
mutex.ReleaseMutex();
 effect : Will lock the contents of the code block , And prevent other threads from entering the code block , Until the code block is completed , Release the lock .

Be careful : Be careful : The defined lock object should be : private + static state + read-only + Object of reference type , This prevents external changes to the lock object .

Mutex It can be system level , So it can span the process .

原网站

版权声明
本文为[Machine vision 001]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240033368794.html