zoukankan      html  css  js  c++  java
  • 信号灯 ManualResetEvent 与 Interlocked.Increment 原子操作使

    class Program
    
        {
    
    
            public static int numb = 0;
    
            public static int numbb = 100;
    
            static ManualResetEvent manu = new ManualResetEvent(false);
    
            static void Main(string[] args)
    
            {
    
                
    
                for (int i = 0; i < numbb; i++)
    
                {
    
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ok), i);
    
                 
    
                 }
    
              
    
    
               //等待事件的完成,即线程调用ManualResetEvent.Set()方法
    
                //eventX.WaitOne  阻止当前线程,直到当前 WaitHandle 收到信号为止。
    
    
                manu.WaitOne(Timeout.Infinite,true);
    
                Console.WriteLine("全部收到信息,完成任务.计数器:{0}",numb);
    
            }
    
            public static  void ok(object x)
    
            {
    
               
    
                for (int i = 0; i < 10; i++)
    
                {
    
                    Console.WriteLine("位置<{0}>,线程ID:<{1}>", i, Thread.CurrentThread.ManagedThreadId);
    
                }
    
    
    
                Interlocked.Increment(ref numb);
    
    
                if (numb==numbb)
    
                {
    
                    manu.Set(); //将事件状态设置为终止状态,允许一个或多个等待线程继续。
    
                }
    
            }
    
          
    
        }
  • 相关阅读:
    缓存服务器
    Consistent Hashing算法-搜索/负载均衡
    MinHash算法-复杂度待整理
    搜索引擎spam
    C语言字节对齐
    关于访问权限的问题
    计蒜客button 概率
    LightOJ
    LightOJ
    LightOJ
  • 原文地址:https://www.cnblogs.com/larva/p/7222336.html
Copyright © 2011-2022 走看看