AutoResetEvent.How to use it

转载 2007年09月16日 21:28:00
AutoResetEvent类:通知正在等待的线程已发生事件,允许线程通过发信号互相通信。通常,此通信涉及线程需要独占访问的资源。  
   
   
  class   MyMainClass  
          {  
                  //Initially   not   signaled.  
              const   int   numIterations   =   100;  
              static   AutoResetEvent   myResetEvent   =   new   AutoResetEvent(false);  
              static   int   number;  
               
              static   void   Main()  
                  {  
                    //Create   and   start   the   reader   thread.  
                    Thread   myReaderThread   =   new   Thread(new   ThreadStart(MyReadThreadProc));  
                    myReaderThread.Name   =   "ReaderThread";  
                    myReaderThread.Start();  
   
                    for(int   i   =   1;   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(0);  
                    }  
   
                    //Terminate   the   reader   thread.  
                    myReaderThread.Abort();  
              }  
   
              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);  
                    }  
              }  
          }   
 

What is reflection and how to use it in CSharp ?

What is reflection and how to use it in CSharp ?

How to Best Use Collaboration IT

How to Best Use Collaboration IT

How to build & install GLFW 3 and use it in a Linux project

GLFW3 This may not be the correct place for this “question”, indeed it is actually an answer, or I...

C_C++随机数据生成(is how to use but not how it work)

随机数据的生成

What is a cronjob and how do I use it?

The cron daemon is a long running process that executes commands at specific dates and times. To sch...

如何用gcc/g++编译生成so链接库,以及how to use .so

(3年前) 由于以后要开发一个mysql的.so,所以该事例名字为my,类名为mysql。 为了说的更明白,mysql里面有3种成员函数,int , unsigned int, char *, 每个...

How to Use Google AdSense Ads on Responsive Websites

How to Use Google AdSense Ads on Responsive Websites Learn how to make your Google AdSense ads resp...

How to Use Oracle Restart in Oracle 11gR2

Oracle Restart是数据库组件监控方法向单实例数据库中的延展,之前srvctl命令可以用来管理RAC数据库中的各个组件,安装了Oracle Restart之后,对于单实例数据库组件(包括数据...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:AutoResetEvent.How to use it
举报原因:
原因补充:

(最多只允许输入30个字)