首页 > 编程语言 > 详细

C# 线程同步

时间:2021-08-18 15:10:21      阅读:17      评论:0      收藏:0      [点我收藏+]

测试线程同步机制 AutoResetEvent:

 1         static AutoResetEvent wakeup = new AutoResetEvent(false);
 2         static void pthread_func()
 3         {
 4             while (true)
 5             {
 6 
 7                 Console.WriteLine("等待唤醒");
 8                 wakeup.WaitOne();
 9                 Console.WriteLine("线程唤醒");
10                 Thread.Sleep(1000);
11             }
12         }
13         static void Main(string[] args)
14         {
15             Thread tmp = new Thread(pthread_func);
16             tmp.Start();
17             Thread.Sleep(1000);
18             Console.WriteLine("发送唤醒");
19             wakeup.Set();
20             Thread.Sleep(2000);
21             Console.WriteLine("发送唤醒");
22             Thread.Sleep(2000);
23             wakeup.Set();
24         }

终端显示:

技术分享图片

 

C# 线程同步

原文:https://www.cnblogs.com/chenxiaolinembed/p/15155959.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!