zoukankan      html  css  js  c++  java
  • 线程同步中使用信号量AutoResetEvent

    using System;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var p = new Program();
                p.Do();
                p.Signal();
            }
    
            AutoResetEvent autoResetEvent = new AutoResetEvent(false); //false代表默认中阻塞状态
    
            void Do()
            {
                var worker = new Thread(() =>
                {
                    Console.WriteLine("Start worker");
                    Console.WriteLine("wait");
                    autoResetEvent.WaitOne(); //等待信号
                    Console.WriteLine("do");
                });
                worker.Start();
            }
    
            void Signal()
            {
                Console.WriteLine("Sent signal");
                Console.ReadKey();
                autoResetEvent.Set(); //发送信号
                Console.ReadKey();
            }
        }
    }

    ManualResetEvent需要手动设定阻塞状态设置为false

  • 相关阅读:
    bzoj4289
    bzoj3033
    bzoj3144
    896C
    bzoj4430
    bzoj4455
    bzoj5117
    BZOJ 1564: [NOI2009]二叉查找树
    BZOJ1261: [SCOI2006]zh_tree
    BZOJ1090: [SCOI2003]字符串折叠
  • 原文地址:https://www.cnblogs.com/goodspeed/p/4068408.html
Copyright © 2011-2022 走看看