Sunday, January 22, 2012

Why Mutex is faster than semaphore?

I do not think that this is true for all OS.
In some OS, It may be true because of the implementation. For example, in QNX, it is written as follows:

--------------------------------------------------------------------------------
 Note that in general, mutexes are much faster than semaphores, which always require a kernel entry. Semaphores don't affect a thread's effective priority; if you need priority inheritance, use a mutex. For more information, see "Mutexes: mutual exclusion locks," earlier in this chapter.
--------------------------------------------------------------------------------
On most processors, acquisition of a mutex doesn't require entry to the kernel for a free mutex. What allows this is the use of the compare-and-swap opcode on x86 processors and the load/store conditional opcodes on most RISC processors.
Entry to the kernel is done at acquisition time only if the mutex is already held so that the thread can go on a blocked list; kernel entry is done on exit if other threads are waiting to be unblocked on that mutex. This allows acquisition and release of an uncontested critical section or resource to be very quick, incurring work by the OS only to resolve contention.
--------------------------------------------------------------------------------

Since because of the nature of the Mutex, only one task can aquire and only the owner can unlock it, the implementation may be very simple. Just flip a bit between 0 and 1, using hardware instructions.
Since the scope of the semaphore is larger, such as number of counts and waking up of other tasks, the implementation may be little complex and may consume more time than mutex.

No comments:

Post a Comment