Mutex: Difference between revisions

m
{{out}}
m ({{out}})
Line 2:
[[Category:Encyclopedia]]
 
A '''mutex''' (''abbreviated'' '''Mut'''ually '''Ex'''clusive access) is a synchronization object, a variant of [[semaphore]] with ''k''=1. A mutex is said to be seized by a [[task]] decreasing ''k''. It is released when the task restores ''k''. Mutexes are typically used to protect a shared resource from concurrent access. A [[task]] seizes (or acquires) the mutex, then accesses the resource, and after that releases the mutex.
A mutex is said to be seized by a [[task]] decreasing ''k''.
It is released when the task restores ''k''. Mutexes are typically used to protect a shared resource from concurrent access.
A [[task]] seizes (or acquires) the mutex, then accesses the resource, and after that releases the mutex.
 
A mutex is a low-level synchronization primitive exposed to deadlocking. A deadlock can occur with just two tasks and two mutexes (if each task attempts to acquire both mutexes, but in the opposite order). Entering the deadlock is usually aggravated by a [[race condition]] state, which leads to sporadic hangups, which are very difficult to track down.
Entering the deadlock is usually aggravated by a [[race condition]] state, which leads to sporadic hangups, which are very difficult to track down.
 
=Variants of mutexes=
Line 370 ⟶ 374:
fmt.Println(value)
}</lang>
{{out}}
Output:
<pre>
2
</pre>
Read-write mutex is provided by the <tt>sync.RWMutex</tt> type. For a code example using a RWMutex, see [[Atomic updates#RWMutex]].
For a code example using a RWMutex, see [[Atomic updates#RWMutex]].
 
===Channels===
If a mutex is exactly what you need, sync.Mutex is there. As soon as things start getting complicated though, Go channels offer a much clearer alternative. As a gateway from mutexes to channels, here is the above program implemented with channels:
As soon as things start getting complicated though, Go channels offer a much clearer alternative.
As a gateway from mutexes to channels, here is the above program implemented with channels:
<lang go>package main
 
Line 552 ⟶ 559:
:- end_object.
</lang>
{{out}}
Sample output:
<pre>
<lang text>
?- slow_print::start.
abc
Line 565 ⟶ 572:
abc
...
</langpre>
 
=={{header|Nim}}==
For mutexes (called locks in Nim) threads support is required, so compile using <code>nim --threads:on c mutex</code>
so compile using <code>nim --threads:on c mutex</code>
 
Creating a mutex:
Line 766 ⟶ 774:
 
=={{header|Python}}==
Demonstrating semaphores.
Demonstrating semaphores. Note that semaphores can be considered as a multiple version of mutex; while a mutex allows a singular exclusive access to code or resources, a semaphore grants access to number of threads up to certain value.<lang Python>import threading
Note that semaphores can be considered as a multiple version of mutex;
while a mutex allows a singular exclusive access to code or resources,
a semaphore grants access to a number of threads up to certain value.
 
<lang Python>import threading
from time import sleep
 
Line 799 ⟶ 812:
=={{header|Racket}}==
 
Racket has semaphores which can be used as mutexes in the usual way. With other language features this can be used to implement new features -- for example, here is how we would implement a protected-by-a-mutex function:
With other language features this can be used to implement new features -- for example, here is how we would implement a protected-by-a-mutex function:
<lang racket>
(define foo
Line 820 ⟶ 834:
(... do something ...))</lang>
 
But more than just linguistic features, Racket has many additional synchronization tools in its VM. Some notable examples: OS semaphore for use with OS threads, green threads, lightweight OS threads, and heavyweight OS threads, synchronization channels, thread mailboxes, CML-style event handling, generic synchronizeable event objects, non-blocking IO, etc, etc.
Some notable examples:
OS semaphore for use with OS threads, green threads, lightweight OS threads, and heavyweight OS threads, synchronization channels, thread mailboxes, CML-style event handling, generic synchronizeable event objects, non-blocking IO, etc, etc.
 
=={{header|Ruby}}==
Line 883 ⟶ 899:
 
=={{header|zkl}}==
zkl has two mutex objects, Lock (mutex) and WriteLock a mutex that allows multiple readers but only one writer. The critical keyword fences code to ensure the lock is released when the code is done.
The critical keyword fences code to ensure the lock is released when the code is done.
<lang zkl>var lock=Atomic.Lock(); lock.acquire(); doSomething(); lock.release();
critical(lock){ doSomething(); }</lang>
Anonymous user