A mutex preferably works in the kernel of the operating system, while maintaining the amount of code around it as short as possible, so you can avoid being disconnected when switching tasks to another process. The exact implementation is therefore a bit secret. It's not hard. This is basically an object that has a logical field that it receives and sets.
- When using a counter, it can become a semaphore.
- A mutex is the starting point for a critical section that internally uses the mutex to see if it can enter the code section. If the mutex is free, it installs the mutex and executes the code, but only when the mutex is disabled. When a critical section notices that a mutex is locked, it may wait for the mutex to exit.
There are wrappers around the underlying mutex logic to wrap it in an object. Then add more wrapper objects so that they are accessible outside the kernel. And then another shell to make it available in .NET. And then several programmers will write their own wrapper code around all this for their own logical needs. The wrappers around the wrappers really make them a muddy territory.
Now, with this basic knowledge of the internal elements of the mutexes, I hope that you are going to use one implementation that relies on the kernel and the hardware underneath. They would be the most reliable. (If the hardware supports them.) If the mutex that you use does not work at this kernel / hardware level, it may still be reliable, but I would advise against using it if there is no alternative.
As far as I know, Windows, Linux and .NET will use kernel / hardware level mutexes.
The Wikipedia page I linked to explains more about internal logic and possible implementations. Preferably, the mutex is controlled by hardware, thereby doing all the reception / tuning of the mutex . (Just to make sure the system does not switch intermediate tasks).
Wim ten Brink Sep 28 '09 at 8:52 2009-09-28 08:52
source share