Mutex: Difference between revisions

26 bytes removed ,  5 months ago
m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(2 intermediate revisions by 2 users not shown)
Line 3:
[[Category:Encyclopedia]]
 
A '''mutex''' (''abbreviated''from "'''Mutmut'''uallyual '''Exex'''clusive accessclusion") 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.
Line 1,232:
 
$lock.protect: { your-ad-here() }</syntaxhighlight>
Locks are reentrant. You may explicitly lock and unlock them, but the syntax above guarantees the lock will be unlocked on scope exit, even if by thrown exception or other exotic control flow. That being said, direct use of locks is discouraged in Perl 6Raku in favor of promises, channels, and supplies, which offer better composable semantics.
 
=={{header|Ruby}}==
Line 1,395:
However, to avoid excessive latency, a VM whose thread were continuing would need to signal to the host that it no longer needed the resource so the lock could be released thereby making it available to the other VM(s). Typically, a shared resource might need to be represented something like this in a Wren script:
 
<syntaxhighlight lang="ecmascriptwren">foreign class Resource {
// obtain a pointer to the resource when available
construct new() {}
9,485

edits