Monday, August 20, 2007

WaitHandle hierarchy

Here is a reflector screenshot of the relationships among the different synchronization object. The wait handle is a simple class whose sole purpose is to wrap a windows kernel object.

WaitHandleDelegate

As you can see, EventWaitHandle, Mutex, and Semaphore are all inheriting from the wait handle object. When a thread is waiting on the kernel object, the thread must always transition from the user mode to the kernel mode, causing the thread to incur a performance hit. So if you want to do the thread synchronization inside an AppDomain, it's best that you simply use the Monitor, which is much more efficient than the kernel object. However, if you have to do the synchronization between different appdomains, processes, you have to use those windows kernel objects.

No comments: