AutoResetEvent的基本用法
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的基本用法的更多相关文章
- AutoResetEvent控制线程用法
本文主要来自一道面试题,由于之前对AutoResetEvent的用户很模糊(即使已经使用过了).面试题题目很简洁:两个线程交替打印0~100的奇偶数.你可以先动手试试,我主要是尝试在一个方法里面完成这 ...
- C# 死锁 Task/AutoResetEvent
与之前<C# 死锁 TaskCompletionSource>类似,还有很多死锁的案例 使用Task异步转同步时,使用不当造成的死锁 private void Task_OnClick(o ...
- C#多线程的用法8-线程间的协作AutoResetEvent
AutoResetEvent自动重置事件,与ManualResetEvent是相对的而言.它同样用于线程间同步,请对照<C#多线程的用法7-线程间的协作ManualResetEvent>进 ...
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
先看一段代码: public class WaitHandlerExample { public static AutoResetEvent waitHandler; ...
- 浅谈AutoResetEvent的用法
using System;using System.Threading; namespace AutoResetEvent_Examples{ class MyMainClass { ...
- AutoResetEvent ManualResetEvent WaitOne使用注意事项
公司还用这些老家伙没办法,用了几次这俩.每次用都要重新翻一下A片. 好好的A片楞是翻译成了禅经.把这东西弄成个玄学.微软也是吃枣药丸.参考了@风中灵药的blog.写的牛逼. 还有一些公司用到的风中灵药 ...
- System.Threading.Timer 定时器的用法
System.Threading.Timer 是C# 中的一个定时器,可以定时(不断循环)执行一个任务.它是在线程上执行的,具有很好的安全性.为此 .Net Framework 提供了5个重载的构造 ...
- [转]AutoResetEvent 与 ManualResetEvent区别
在C#多线程编程中,这两个类几乎是不可或缺的,他们的用法/声明都很类似,那么区别在哪里了? Set方法将信号置为发送状态 Reset方法将信号置为不发送状态 WaitOne等待信号的发送 其实,从名字 ...
- C#多线程之二:ManualResetEvent和AutoResetEvent
初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...
随机推荐
- Django学习之mysql结果显示
背景:向数据库添加相同名字的用户 上节可知,在查询某个用户信息时,只能查看到行数但不能查看结构性信息. fetchone()函数可以但仅可逐个查看结构性信息.查不到信息将以None替补. fetchA ...
- linux进程篇 (三) 进程间的通信3 IPC通信
3 IPC通信 用户空间 进程A <----无法通信----> 进程B -----------------|--------------------------------------|- ...
- 基于傅里叶变换的音频重采样算法 (附完整c代码)
前面有提到音频采样算法: WebRTC 音频采样算法 附完整C++示例代码 简洁明了的插值音频重采样算法例子 (附完整C代码) 近段时间有不少朋友给我写过邮件,说了一些他们使用的情况和问题. 坦白讲, ...
- 关于verilog中的signed类型
在数字电路中,出于应用的需要,我们可以使用无符号数,即包括0及整数的集合:也可以使用有符号数,即包括0和正负数的集合.在更加复杂的系统中,也许这两种类型的数,我们都会用到. 有符号数通常以2的补码形式 ...
- services 系统服务的启动、停止、卸载
MySQL服务的启动.停止与卸载 在 Windows 命令提示符下运行: 启动: net start MySQL 停止: net stop MySQL 卸载: sc delete MySQL Sc d ...
- ubuntu16安装过程中的硬盘分区
硬盘分区主要是三个区,如下图,分别是/,swap交换空间,/home,其中第一个分区/是主分区,建议至少8GB,要不ubuntu安装的时候放不下.
- 【公司动态添加行】前台穿一个json的字符串到后台,并解析
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- 「题目代码」P1060~P1065(Java)
P1060 谭浩强C语言(第三版)习题7.5 注意行末空格. import java.util.*; import java.io.*; import java.math.*; import java ...
- 使用unittest里面的discover()方法组织测试用例
import osimport unittest directory = os.getcwd()# 测试用例的目录organize = unittest.defaultTestLoader.disco ...
- katalon系列三:Project Setting-项目设置
安装完katalon后,用QQ邮箱注册并登陆,然后新建一个项目.点击菜单Project-Project Setting打开项目设置,接下来介绍几个你可能会用到的设置. 1.Text Design-We ...