您的位置:首页 > 编程语言 > C#

关于C#中AutoResetEvent和ManualResetEvent的一点学习心得

2012-07-26 17:10 681 查看
C#中的AutoResetEvent和ManualResetEvent用于实现线程同步。其基本工作原理是多个线程持有同一个XXXResetEvent,在这个XXXResetEvent未被set前,各线程都在WaitOne()除挂起;在这个XXXResetEvent被set后,所有被挂起的线程中有一个(AutoResetEvent的情况下)或全部(ManualResetEvent的情况下)恢复执行。

AutoResetEvent与ManualResetEvent的差别在于某个线程在WaitOne()被挂起后重新获得执行权时,是否自动reset这个事件(Event),前者是自动reset的,后者不是。所以从这个角度上也可以解释上段提到的“在这个XXXResetEvent被set后,所有被挂起的线程中有一个(AutoResetEvent的情况下)或全部(ManualResetEvent的情况下)恢复执行”——因为前者一旦被某个线程获取后,立即自动reset这个事件(Event),所以其他持有前者的线程之后WaitOne()时又被挂起;而后者被某个获取后,不会自动reset这个事件(Event),所以后续持有后者的线程在WaitOne()时不会被挂起。

C-sharp代码

namespace AutoResetEvent_Examples {

class MyMainClass {

/*

* 构造方法的参数设置成false后,表示创建一个没有被set的AutoResetEvent

* 这就导致所有持有这个AutoResetEvent的线程都会在WaitOne()处挂起

* 此时如果挂起的线程数比较多,那么你看一下自己的内存使用量……。

* 如果将参数设置成true,表示创建一个被set的AutoResetEvent

* 持有这个AutoResetEvent的线程们会竞争这个Event

* 此时,在其他条件满足的情况下

* 至少会有一个线程得到执行

* 而不是因得不到Event而导致所有线程都得不到执行

*/

static AutoResetEvent myResetEvent = new AutoResetEvent(false);

static int _Count = 0;

static void Main() {

Thread myThread = null;

for(int i = 0;i < 100;i++) {

myThread = new Thread(new ThreadStart(MyThreadProc));

myThread.Name = "Thread" + i;

myThread.Start();

}

myResetEvent.Set();

Console.Read();

}

static void MyThreadProc() {

myResetEvent.WaitOne();

_Count++;

Console.WriteLine("In thread:{0},label={1}.",Thread.CurrentThread.Name,_Count);

myResetEvent.Set();

}

}

}

C-sharp代码

namespace ManualResetEvent_Examples {

class MyMainClass {

/*

* 构造方法的参数设置成false后,表示创建一个没有被set的ManualResetEvent

* 这就导致所有持有这个ManualResetEvent的线程都会在WaitOne()处挂起

* 此时如果挂起的线程数比较多,那么你看一下自己的内存使用量……。

* 如果将参数设置成true,表示创建一个被set的ManualResetEvent

* 持有这个ManualResetEvent的线程们在其他条件满足的情况下

* 会同时得到执行(注意,是同时得到执行!所以本例中的_Count的结果一般是不正确的^_^)

* 而不是因得不到Event而导致所有线程都得不到执行

*/

static ManualResetEvent myResetEvent = new ManualResetEvent(false);

static int _Count = 0;

static void Main() {

Thread myThread = null;

for(int i = 0;i < 1000;i++) {

myThread = new Thread(new ThreadStart(MyThreadProc));

myThread.Name = "Thread" + i;

myThread.Start();

}

myResetEvent.Set();

Console.Read();

}

static void MyThreadProc() {

myResetEvent.WaitOne();

_Count++;

/*

* 在new ManualResetEvent(false);的情况下

* 下面的输出结果可能比较诡异:多个线程都输出label=1000!

* 一种可能的原因是多个线程在各自执行到_Count++后,被挂起

* 随后打印的_Count值就不是本线程中刚刚修改过的_Count值了。

*/

Console.WriteLine("In thread:{0},_Count={1}.",Thread.CurrentThread.Name,_Count);

}

}

}

set是让事件(Event)发生,而reset是让事件(Event)复位或者说忽略已经的事件(Event)。WaitOne是等待事件(Event)的发生,之后继续向下执行,否则一直等待。

在构造AutoResetEvent和ManualResetEvent的时候,它们的构造方法里需要一个参数initState,中文版MSDN(2005和2008)上的解释是“若要将初始状态设置为终止,则为 true;若要将初始状态设置为非终止,则为false。”,我看了一个下午,没弄明白,而看一下英文版后大概就明白了“A value that you set totrueto set the initial state of the specified event to signaled. Set this value tofalseto set the initial state of the event to nonsignaled.”(参见:http://msdn.microsoft.com/en-us/library/ee432364.aspx),大体意思是说这个参数决定是否在构造这个Event的时候就设置它为“发生”状态(signaled),如果是,则设置为true,也就是说持有这个Event的一个或多个线程在一开始就可以执行,而不需要挂起,至少是不会全部挂起(持有AutoResetEvent的一个或多个线程在任意时刻至多有一个线程在执行;持有ManualResetEvent的一个或多个线程会同时执行),否则为false(持有AutoResetEvent和ManualResetEvent的所有线程都将挂起,因为事件(Event)没有被set,即事件没有发生)。

另外稍微提一下,我在做多线程测试的时候,发现在线程数少的情况下,即使多个线程不做任何同步,如果对一个公共变量进行非互斥式修改时,不会至少很难出现不一致的情况,比如开100个线程,这个线程不做任何同步就分别给一个公共变量执行加1操作,那么结果在绝绝绝大部分的情况下是100!所以,我最后就下了狠心,把线程数增加到1000个,这个时候才出现问题,但问题也不是想象得那么严重——结果在991-1000之间!

再有,MSDN上对Monitor的Wait和Pulse两个方法用法的举例会导致死锁,一种死锁的执行顺序是:

1、线程tSecond在SecondThread()中执行到while(Monitor.Wait(m_smplQueue,1000))后,释放m_smplQueue的锁,线程tSecond挂起;

2、线程tFirst在FirstThread()中执行到Monitor.Wait(m_smplQueue)之前耗费的时间超过1000毫秒,此时线程tSecond退出,线程tFirst挂起,并且从此以后不会被恢复!

可以使用如下改动过的代码验证:

C-sharp代码

public void FirstThread() {

int counter = 0;

lock(m_smplQueue) {

Console.WriteLine("11");

while(counter < MAX_LOOP_TIME) {

//Wait, if the queue is busy.

Console.WriteLine("12");

Monitor.Wait(m_smplQueue);

Console.WriteLine("13");

//Push one element.

m_smplQueue.Enqueue(counter);

Console.WriteLine("14");

//Release the waiting thread.

Monitor.Pulse(m_smplQueue);

Console.WriteLine("15");

counter++;

Console.WriteLine("16");

}

}

}

public void SecondThread() {

lock(m_smplQueue) {

Console.WriteLine("21");

//Release the waiting thread.

Monitor.Pulse(m_smplQueue);

Console.WriteLine("22");

//Wait in the loop, while the queue is busy.

//Exit on the time-out when the first thread stops.

while(Monitor.Wait(m_smplQueue,1000)) {

Console.WriteLine("23");

//Pop the first element.

int counter = (int) m_smplQueue.Dequeue();

Console.WriteLine("24");

//Print the first element.

Console.WriteLine(counter.ToString());

Console.WriteLine("25");

//Release the waiting thread.

Monitor.Pulse(m_smplQueue);

Console.WriteLine("26");

}

Console.WriteLine("27");

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: