首先lock和Minitor有什么區別呢?
其實lock在IL代碼中會被翻譯成Monitor。也就是Monitor.Enter(obj)和Monitor.Exit(obj).
lock(obj)
{
}
等價為:
try
{
Monitor.Enter(obj)
}
catch()
{}
finally
{
Monitor.Exit(obj)
}
所以lock能做的,Monitor肯定能做,Monitor能做的,lock不一定能做。那么Monitor額外的功能呢?
1:Monitor.TryEnter(obj,timespan)----timeout之后,就不執行這段代碼了。lock可是一直會死等的。
2:還有Monitor.Wait()和Monitor.Pulse()。在lock代碼里面如果調用了Monitor.Wait(),會放棄對資源的所有權,讓別的線程lock進來。然后別的線程代碼里Pulse一下(讓原線程進入到等待隊列),然后在Wait一下釋放資源,這樣原線程的就可以繼續執行了(代碼還堵塞在wait那句話呢)。
也就是說,必須兩個或多個線程共同調用Wait和Pulse,把資源的所有權拋來拋去,才不會死鎖。
和AutoEvent相似是處理同步關系的,但是AutoEvent是跨進程的,而Monitor是針對線程的。
以下是MSDN的代碼示例,調試起來很容易看出來兩個函數的作用了,因為盡管是多線程程序,但是是同步操作,所以代碼始終是單步執行的。
using System;
using System.Threading;
using System.Collections;
namespace MonitorCS1
{
class MonitorSample
{
const int MAX_LOOP_TIME = 100;
Queue m_smplQueue;
public MonitorSample()
{
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, 50000))
{
//Pop the first element.
int counter = (int)m_smplQueue.Dequeue();
//Print the first element.
Console.WriteLine(counter.ToString());
//Release the waiting thread.
Monitor.Pulse(m_smplQueue);
}
}
}
//Return the number of queue elements.
public int GetQueueCount()
{
return m_smplQueue.Count;
}
static void Main(string[] args)
{
//Create the MonitorSample object.
MonitorSample test = new MonitorSample();
//Create the first thread.
Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
//Create the second thread.
Thread tSecond = new Thread(new ThreadStart(test.SecondThread));
//Start threads.
tFirst.Start();
tSecond.Start();
//wait to the end of the two threads
tFirst.Join();
tSecond.Join();
//Print the number of queue elements.
Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString());
}
}
}