The following example uses an AutoResetEvent to synchronize the activities of two threads.The first thread, which is the application thread, executes Main.It writes values to the protected resource, which is a static (Shared in Visual Basic) field named number.The second thread executes the static ThreadProc method, which reads the values written by Main.

The ThreadProc method waits for the AutoResetEvent.When Main calls the Set method on the AutoResetEvent, the ThreadProc method reads one value.The AutoResetEvent immediately resets, so the ThreadProc method waits again.

The program logic guarantees that the ThreadProc method will never read the same value two times.It does not guarantee that theThreadProc method will read every value written by Main.That guarantee would require a second AutoResetEvent lock.

After each write operation, Main yields by calling the Thread.Sleep method, to give the second thread a chance to execute.Otherwise, on a single-processor computer Main would write many values between any two read operations.

    class Program
{
//Initially not signaled.
private const int NumIterations = ; private static readonly AutoResetEvent MyResetEvent = new AutoResetEvent(false); private static int _number; public static void Main()
{
//Create and start the reader thread.
var myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start(); for (int i = ; i <= NumIterations; i++)
{
Console.WriteLine("Writer thread writing value: {0}", i);
_number = i; //Signal that a value has been written.
MyResetEvent.Set(); //Give the Reader thread an opportunity to act.
Thread.Sleep();
} //Terminate the reader thread.
myReaderThread.Abort(); Console.ReadKey();
} private static void MyReadThreadProc()
{
while (true)
{
//The value will not be read until the writer has written
// at least once since the last read.
MyResetEvent.WaitOne();
Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, _number);
}
}
}

AutoResetEvent的基本用法的更多相关文章

  1. AutoResetEvent控制线程用法

    本文主要来自一道面试题,由于之前对AutoResetEvent的用户很模糊(即使已经使用过了).面试题题目很简洁:两个线程交替打印0~100的奇偶数.你可以先动手试试,我主要是尝试在一个方法里面完成这 ...

  2. C# 死锁 Task/AutoResetEvent

    与之前<C# 死锁 TaskCompletionSource>类似,还有很多死锁的案例 使用Task异步转同步时,使用不当造成的死锁 private void Task_OnClick(o ...

  3. C#多线程的用法8-线程间的协作AutoResetEvent

    AutoResetEvent自动重置事件,与ManualResetEvent是相对的而言.它同样用于线程间同步,请对照<C#多线程的用法7-线程间的协作ManualResetEvent>进 ...

  4. 一个实例明白AutoResetEvent和 ManulResetEvent的用法

    先看一段代码: public class WaitHandlerExample {                 public static AutoResetEvent waitHandler;  ...

  5. 浅谈AutoResetEvent的用法

    using System;using System.Threading; namespace AutoResetEvent_Examples{    class MyMainClass    {    ...

  6. AutoResetEvent ManualResetEvent WaitOne使用注意事项

    公司还用这些老家伙没办法,用了几次这俩.每次用都要重新翻一下A片. 好好的A片楞是翻译成了禅经.把这东西弄成个玄学.微软也是吃枣药丸.参考了@风中灵药的blog.写的牛逼. 还有一些公司用到的风中灵药 ...

  7. System.Threading.Timer 定时器的用法

    System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此  .Net Framework 提供了5个重载的构造 ...

  8. [转]AutoResetEvent 与 ManualResetEvent区别

    在C#多线程编程中,这两个类几乎是不可或缺的,他们的用法/声明都很类似,那么区别在哪里了? Set方法将信号置为发送状态 Reset方法将信号置为不发送状态 WaitOne等待信号的发送 其实,从名字 ...

  9. C#多线程之二:ManualResetEvent和AutoResetEvent

    初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...

随机推荐

  1. 中国大学MOOC-C程序设计(浙大翁恺)—— 时间换算

    时间换算(10分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果小 ...

  2. python3.5 安装twisted

    https://blog.csdn.net/caimouse/article/details/77647952 下载地址:http://www.lfd.uci.edu/~gohlke/pythonli ...

  3. java-执行流程控制语句

    就像有知觉的生物一样,程序必须在执行的过程中控制它的世界,并做出选择.java使用执行流程控制语句做出选择. 1.选择语句 if if(布尔表达式){ 业务逻辑1; }else{ 业务逻辑2: } s ...

  4. c#调用c++库函数

    如果是非托管的,就用DllImport,举例    using System;    using System.Runtime.InteropServices;    class MainApp    ...

  5. 成都Uber优步司机奖励政策(2月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  6. 【转载】SOCKS代理:从***到内网漫游

    原文:SOCKS代理:从***到内网漫游 本文原创作者:tahf,本文属FreeBuf原创奖励计划,未经许可禁止转载 之前在Freebuf上学习过很多大牛写的关于Tunnel.SOCKS代理.***等 ...

  7. 【Hadoop】Seondary NameNode不是备份NameNode!!

    昨天和舍友聊天时无意中提起Secondary NameNode,他说这是备用NameNode.我当时就有点疑惑..之后查阅了相关资料和博客,算是基本理解了什么是Secondary NameNode. ...

  8. JVM常见配置

    堆设置 -Xms:初始堆大小 -Xmx:最大堆大小 -XX:NewSize=n:设置年轻代大小 -XX:NewRatio=n:设置年轻代和年老代的比值.如:为3,表示年轻代与年老代比值为1:3,年轻代 ...

  9. Linux命令应用大词典-第22章 GRUB

    22.1 grub-md5-crypt:使用MD5格式加密口令 22.2 grub-install:在设备上安装GRUB 22.3 grub:进入GRUB命令shell 22.4 grub-crypt ...

  10. Objective-C 封装 继承 多态

    封装 #import <Foundation/Foundation.h> @interface Person : NSObject { //@public int _age; } - (v ...