What is the basic concept behind WaitHandle?

前端 未结 5 1853
清歌不尽
清歌不尽 2020-12-04 14:04

What is the basic concept behind WaitHandle in C# .net threading? Whats is its use? When to use it? What is the use of WaitAll and Wait

5条回答
  •  暖寄归人
    2020-12-04 14:44

    It is an abstract class, you don't use it directly. Concrete derived classes are ManualResetEvent, AutoResetEvent, Mutex and Semaphore. Important classes in your toolbox to implement thread synchronization. They inherit the WaitOne, WaitAll and WaitAny methods, you use them to detect that one or more threads signaled the wait condition.

    Typical usage scenario for Manual/AutoResetEvent is to tell a thread to exit or to let a thread signal that it has progressed to an important sequence point. Semaphore helps you to limit the number of threads that perform an action. Or to implement threading synchronization that should not have affinity to a particular thread. Mutex is there to assign ownership to a section of code to one thread, the lock statement is often applicable there as well.

    Books have been written about it. Joe Duffy's Concurrent Programming in Windows is the latest and greatest. Strongly recommended if you contemplate writing threaded code.

提交回复
热议问题