c# Monitor线程锁代码
代码语言:c#
所属分类:通讯
代码描述:c# Monitor线程锁代码
代码标签: 锁
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
using System; using System.Threading; using System.Collections; namespace HelloWorldApplication { class HelloWorld { const int MAX_LOOP_TIME = 5; Queue m_smplQueue; public HelloWorld() { m_smplQueue = new Queue(); } public void FirstThread() { int counter = 0; lock (m_smplQueue) { while (counter < MAX_LOOP_TIME) { //Wait, if the queue is busy. Monitor.Wait(m_smplQueue); //Push one element. m_smplQueue.Enqueue(counter); //Release the waiting thread. Monitor.Pulse(m_smplQueue); counter++; } int i = 0; } } public void SecondThread() { lock (m_smplQueue) { //Release the waiting thread. Monitor.Pulse(m_smplQueue); //Wait in the loop, while the queue is busy. //Exit on the time-out when the first thread stops. while (Monitor.Wait(m_smplQueue, 5000)) .........完整代码请登录后点击上方下载按钮下载查看
网友评论0