Mutual Exclusion: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
(Existing page was virtually non-existent, this is at least a start :¬))
(Fleshed out the article a bit and removed stub.)
 
Line 1: Line 1:
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.
{{Stub}}


== What ==
A "mutex", from "mutual exclusion" is a resource used as part of a locking mechanism to ensure that only one thread at a time can access another resource; I.E. to make a code section safe to run on a multi-threaded system.
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.


== Mutex ==
An example implementation might be to check for the existence of a specific file, if this file exists then another thread has already 'locked' access and our example must try again later. However, if the file does not exist, our thread immediately creates it, so 'locking' the resource it is about to acces, then it goes ahead with access on the potentially contested resource, safe in the knowledge that no other thread should be able to interfere.
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 ==
=== Articles ===
* [[Synchronization Primitives]]


[[Category:Synchronization]]
[[Category:Synchronization]]

Latest revision as of 11:24, 28 March 2011

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.

What

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.

Mutex

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

Articles