Mutual Exclusion

From OSDev Wiki
Jump to: navigation, search

Mutual exclusion is a word commonly used in the context of synchronization (e.g. between threads). It is used to ensure that only one instance can access a resource.



Mutual exclusion refers to the fact that multiple instances can attempt to access one resource, but only one of them will be granted access at a time. The word 'instance' here refers to something attempting to gain access to a resource, e.g. a thread.


The principle of mutual exclusion is commonly used in the mutex locking mechanism. A mutex is also referred to as a binary semaphore, because it uses a boolean instead of an integer to test its lock. This lock can then be used in i.e. multi-threaded systems to make a section of the code safe to run on multiple threads.

Suppose multiple threads have to access the same file. You could then use a mutex to determine whether the file is already in use or not. If it is in use, the mutex will be locked and the thread in question will have to wait for it to become available. The other thread will unlock the mutex once it is done with the file. When the first thread notices that the mutex has become unlocked, it immediately locks it and proceeds accessing the file. This way, both threads are certain that they will not intefere with each other or try to access the file at the exact same moment.

Suppose one has to check for the existance of a specific file.

See Also


Personal tools