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. django之路由分组,反向解析,有名,无名分组

    路由层 无名分组 有名分组 反向解析 路由分发 名称空间 伪静态的概念 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'test',vi ...

  2. fake_useragent 封装好user-agent的模块

    from fake_useragent import UserAgent useragent = UserAgent()print(useragent.random)

  3. MCUXpresso release build 时提示GFLIB等函数未引用的问题

    MCUXpresso release build 时提示 GFLIB 等函数未引用的问题 最近在使用 MCUXpresso 编译工程时选择 Debug(Debug build) 能顺利编译,但是选择 ...

  4. C语言实现 "谁是凶手?"

    日本某地发生了一件谋杀案,警察通过排查确定杀人凶手必为4个嫌疑犯的一个.以下为4个嫌疑犯的供词.A说:不是我.   a=0B说:是C.   c=1 C说:是D.      d=1D说:C在胡说    ...

  5. (数据科学学习手札42)folium进阶内容介绍

    一.简介 在上一篇(数据科学学习手札41)中我们了解了folium的基础内容,实际上folium在地理信息可视化上的真正过人之处在于其绘制图像的高度可定制化上,本文就将基于folium官方文档中的一些 ...

  6. 北京Uber优步司机奖励政策(3月11日)

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

  7. 北京Uber优步司机奖励政策(12月15日)

    用户组:人民优步及电动车(适用于12月15日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...

  8. c++继承详解:共有(public)继承,私有继承(private)继承,保护(protected)继承

    公有继承(public)继承.私有继承(private).保护继承(protected)是常用的三种继承方式. 1.公有继承(public) 公有继承的特点是基类的公有成员和保护成员作为派生类的成员时 ...

  9. ORB-SLAM(七)ORBextractor 特征提取

    该类中主要调用OpenCV中的函数,提取图像中特征点(关键点及其描述,描述子,以及图像金字塔) 参考TUM1.yaml文件中的参数,每一帧图像共提取1000个特征点,分布在金字塔8层中,层间尺度比例1 ...

  10. VMWare虚拟机下 centos network is unreachable 问题的解决

    vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static BROADCAST=192.168.1.255 HW ...