zoukankan      html  css  js  c++  java
  • ManualResetEvent的简单理解

    看了很久的ManualResetEvent,但还是没看明白官方的解释,不妨让我们来看看定义的英文注释,或许更容易理解:

    // Summary:
     //     Initializes a new instance of the System.Threading.ManualResetEvent class
     //     with a Boolean value indicating whether to set the initial state to signaled.
     //
     // Parameters:
     //   initialState:
     //     true to set the initial state signaled; false to set the initial state to
     //     nonsignaled.
     [SecuritySafeCritical]
     public ManualResetEvent(bool initialState);
     
     //
     // Summary:
     //     Sets the state of the event to nonsignaled, causing threads to block.
     //
     // Returns:
     //     true if the operation succeeds; otherwise, false.
     [SecuritySafeCritical]
     public bool Reset();
     
     //
     // Summary:
     //     Blocks the current thread until the current System.Threading.WaitHandle receives
     //     a signal, using a 32-bit signed integer to measure the time interval.
     //
     // Parameters:
     //   millisecondsTimeout:
     //     The number of milliseconds to wait, or System.Threading.Timeout.Infinite
     //     (-1) to wait indefinitely.
     //
     // Returns:
     //     true if the current instance receives a signal; otherwise, false.
     //
     // Exceptions:
     //   System.ObjectDisposedException:
     //     The current instance has already been disposed.
     //
     //   System.ArgumentOutOfRangeException:
     //     millisecondsTimeout is a negative number other than -1, which represents
     //     an infinite time-out.
     //
     //   System.Threading.AbandonedMutexException:
     //     The wait completed because a thread exited without releasing a mutex. This
     //     exception is not thrown on Windows 98 or Windows Millennium Edition.
     //
     //   System.InvalidOperationException:
     //     The current instance is a transparent proxy for a System.Threading.WaitHandle
     //     in another application domain.
     public virtual bool WaitOne(int millisecondsTimeout);
     
     //
     // Summary:
     //     Sets the state of the event to signaled, allowing one or more waiting threads
     //     to proceed.
     //
     // Returns:
     //     true if the operation succeeds; otherwise, false.
     [SecuritySafeCritical]
     public bool Set();

    总结:

    • ManualResetEvent 实例时接收一个Boolean类型的参数来初始化其状态,true表示畅通无阻的状态(signaled),线程不能阻塞,即使调用WaitOne也不能 阻塞;false表示是可阻塞的状态(nonsignaled),可以调用WaitOne等方法来阻塞的。这里初始化的只是一个状态。
    • Reset方法把 ManualResetEvent 的状态设置为可阻塞的(nonsignaled),不管之前是什么状态,调用Reset都把状态设置为阻塞的(nonsignaled)。
    • WaitOne 阻塞当前线程直到别的线程调用Set方法,只有ManualResetEvent 的状态是可阻塞(nonsignaled)时,WaitOne才能真正的起到作用;
    出处:http://www.zhaiqianfeng.com    
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Python基础----内置函数补充、匿名函数、递归函数
    为arm-linux开发板挂载基于nfs的根文件系统
    vsftp上传553 Could not create file错误解决
    在itop4412移植linux4.14和设备树遇到的问题及解决
    itop4412uboot中支持usbhub
    上下界网络流
    lca(最近公共祖先(在线)) 倍增法详解
    lca(最近公共祖先(离线))
    最小费用最大流
    spfa模板+讲解
  • 原文地址:https://www.cnblogs.com/zhaiqianfeng/p/4618061.html
Copyright © 2011-2022 走看看