关于AutoResetEvent的使用

本文介绍了AutoResetEvent的基本概念及其在多线程环境中的应用。通过一个买书的场景示例,详细展示了AutoResetEvent如何控制线程之间的同步,以及它与ManualResetEvent的主要区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AutoResetEvent 允许线程通过发信号互相通信。通常,此通信涉及线程需要独占访问的资源。

线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号。如果 AutoResetEvent 处于非终止状态,则该线程阻塞,并等待当前控制资源的线程
通过调用 Set 发出资源可用的信号。

调用 SetAutoResetEvent 发信号以释放等待线程。AutoResetEvent 将保持终止状态,直到一个正在等待的线程被释放,然后自动返回非终止
状态。如果没有任何线程在等待,则状态将无限期地保持为终止状态。

可以通过将一个布尔值传递给构造函数来控制 AutoResetEvent 的初始状态,如果初始状态为终止状态,则为 true ;否则为 false

通俗的来讲只有等myResetEven.Set()成功运行后,myResetEven.WaitOne()才能够获得运行机会;Set是发信号,WaitOne是等待信号,只有发了信号,
等待的才会执行。如果不发的话,WaitOne后面的程序就永远不会执行。下面我们来举一个例子:我去书店买书,当我选中一本书后我会去收费处付钱,
付好钱后再去仓库取书。这个顺序不能颠倒,我作为主线程,收费处和仓库做两个辅助线程,代码如下:

http://www.my400800.cn

using 
System;
using 
System.Linq;
using 
System.Activities;
using 
System.Activities.Statements;
using 
System.Threading;

namespace 
CaryAREDemo
{
    class 
Me
    
{
        const int 
numIterations = 550
;
        static 
AutoResetEvent 
myResetEvent = new 
AutoResetEvent
(false
);
        static 
AutoResetEvent 
ChangeEvent = new 
AutoResetEvent
(false
);
        //static ManualResetEvent myResetEvent = new ManualResetEvent(false);
        //static ManualResetEvent ChangeEvent = new ManualResetEvent(false);
        
static int 
number; //这是关键资源

        
static void 
Main()
        {
            Thread 
payMoneyThread = new 
Thread
(new 
ThreadStart
(PayMoneyProc));
            payMoneyThread.Name = "付钱线程"
;
            Thread 
getBookThread = new 
Thread
(new 
ThreadStart
(GetBookProc));
            getBookThread.Name = "取书线程"
;
            payMoneyThread.Start();
            getBookThread.Start();

            for 
(int 
i = 1
; i <= numIterations; i++)
            {
                Console
.WriteLine("买书线程:数量{0}"
, i);
                number = i;
                //Signal that a value has been written.
                
myResetEvent.Set();
                ChangeEvent.Set();
                Thread
.Sleep(0
);
            }
            payMoneyThread.Abort();
            getBookThread.Abort();
        }

        static void 
PayMoneyProc()
        {
            while 
(true
)
            {
                myResetEvent.WaitOne();
                //myResetEvent.Reset();
                
Console
.WriteLine("{0}:数量{1}"
, Thread
.CurrentThread.Name, number);
            }
        }
        static void 
GetBookProc()
        {
            while 
(true
)
            {
                ChangeEvent.WaitOne();
                // ChangeEvent.Reset();               
                
Console
.WriteLine("{0}:数量{1}"
, Thread
.CurrentThread.Name, number);
                Console
.WriteLine("------------------------------------------"
);
                Thread
.Sleep(0
);
            }
        }
    }
}
运行结果如下:
 

AutoResetEvent与ManualResetEvent的区别

他们的用法\声明都很类似,Set方法将信号置为发送状态 Reset方法将信号置为不发送状态WaitOne等待信号的发送。其实,从名字就可以看出一个手动,
一个自动,这个手动和自动实际指的是在Reset方法的处理上,如下面例子:

public AutoResetEvent autoevent=new AutoResetEvent(true);
public ManualResetEvent manualevent=new ManualResetEvent(true);

默认信号都处于发送状态,

autoevent.WaitOne();
manualevent.WaitOne();

如果 某个线程调用上面该方法,则当信号处于发送状态时,该线程会得到信号,得以继续执行。差别就在调用后,autoevent.WaitOne()每次只允许一个线程
进入,当某个线程得到信号(也就是有其他线程调用了autoevent.Set()方法后)后,autoevent会自动又将信号置为不发送状态,则其他调用WaitOne的线程只
有继续等待.也就是说,autoevent一次只唤醒一个线程。而manualevent则可以唤醒多个线程,因为当某个线程调用了set方法后,其他调用waitone的线程
获得信号得以继续执行,而manualevent不会自动将信号置为不发送.也就是说,除非手工调用了manualevent.Reset().方法,则 manualevent将一直保持有信号状态,manualevent也就可以同时唤醒多个线程继续执行。如果上面的程序换成 ManualResetEvent的话,就需要在waitone后面做下reset。

### .NET 8.0 中 `AutoResetEvent` 的使用 在多线程编程中,同步原语对于控制多个线程之间的访问至关重要。`AutoResetEvent` 是一种用于实现线程间通信的事件对象,在一个线程完成特定操作后通知另一个等待该条件发生的线程。 #### 创建和初始化 `AutoResetEvent` 可以创建两种状态下的实例:初始状态下设置为已触发(即信号量可用),或者未触发(默认情况)。这取决于构造函数接收的一个布尔参数: ```csharp // 已触发的状态 var autoEventTrue = new AutoResetEvent(true); // 未触发的状态 var autoEventFalse = new AutoResetEvent(false); ``` #### 设置和重置事件 通过调用 `Set()` 方法来手动将事件置于已触发状态;当任意数量的等待线程被唤醒并继续执行之后,自动重置回未触发状态。如果没有任何线程正在等待,则此方法不会做任何事情[^2]。 ```csharp autoEvent.Set(); ``` 要使事件进入非激活态,可利用 `Reset()` 函数强制将其变为无信号状态,阻止其他监听者前进直到再次收到新的 Set 调用为止。 ```csharp autoEvent.Reset(); ``` #### 等待事件发生 为了阻塞当前线程直至关联的对象变成有信号或超时时间到达,应该采用如下方式之一来进行挂起处理: - **WaitOne()**: 阻塞调用线程无限期地等待,直到它接收到对应的 set 操作。 ```csharp autoEvent.WaitOne(); ``` - **WaitOne(TimeSpan)** 或 **WaitOne(Int32)** : 增加了一个最大时限参数,允许指定最长等待秒数/毫秒数作为整数值传递给这些版本的方法签名形式。 ```csharp bool result = autoEvent.WaitOne(5000); // 等待五秒钟 if (!result) Console.WriteLine("Timeout expired."); ``` #### 完整示例程序展示如何在线程之间共享资源而互不干扰 下面是一个简单的例子说明了怎样运用上述概念构建一个多生产者单消费者模式的应用场景,其中主线程负责消费数据项,而辅助线程则不断向队列里添加新元素。 ```csharp using System; using System.Threading; public class Program { private static readonly Queue<int> queue = new Queue<int>(); private static readonly AutoResetEvent producerEvent = new AutoResetEvent(false); private static readonly AutoResetEvent consumerEvent = new AutoResetEvent(false); public static void Main(string[] args) { Thread t1 = new Thread(() => Producer()); Thread t2 = new Thread(() => Consumer()); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Console.ReadLine(); } private static void Producer(){ int count = 0; while (count++ < 10){ lock(queue){ queue.Enqueue(count); Console.WriteLine($"Produced item {count}"); } producerEvent.Set(); // Notify the consumer thread that an item is available. consumerEvent.WaitOne(); // Wait until notified by the consumer to continue producing items. } } private static void Consumer(){ do{ producerEvent.WaitOne(); // Wait for notification from the producer. lock(queue){ if(queue.Count > 0){ int consumedItem = queue.Dequeue(); Console.WriteLine($"Consumed item {consumedItem}"); if(consumedItem >= 10){ break;} // Exit after consuming all produced items. } } consumerEvent.Set(); // Signal back to allow further production of items. }while(true); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值