What is the difference between Mutex and Critical Section?
In one phrase mutex is designed to be an inter process synchronization object while critical section was made to provide synchronization of threads of the same process.
-
When any operation on mutex is performed then a thread will switch to kernel mode regardless of
the state of the mutex (owned or free). However when using critical section, kernel mode will be entered only if critical section is already owned by another thread.
Thus critical sections are generally faster. -
Mutex is a named kernel object.
Thus you can use one mutex in different processes by using the same name or by duplicating the handle for all processes.
This allows mutex to work as inter process synchronization mechanism.
On the other hand, critical sections are shared only between threads of the same process. -
It is possible to access members of the CRITICAL_SECTION structure.
For example, checking RecursionCount and OwningThread might be useful for debugging.
However with mutex you cannot get any information about the owner or monitor multiple owning.