测试线程同步机制 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 }
终端显示: