Anonymous user
Mutex: Difference between revisions
Add source for Rust
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
(Add source for Rust) |
||
Line 294:
task() ->
loop() ->
mutex_acquire( Pid ) ->
mutex_release( Pid ) -> Pid ! {release, erlang:self()}.
Line 318:
print( _Mutex, _N, 0 ) -> ok;
print( Mutex, N, M ) ->
print_slow( X ) ->
</lang>
{{out}}
Line 768:
meth test
lock
end
end
Line 895:
class res_thread(threading.Thread):
# after done with resource, return it to pool and flag so
# create 4 threads, each acquire resorce and work
for i in range(1, 5):
=={{header|Racket}}==
Line 950:
$lock.protect: { your-ad-here() }</lang>
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
=={{header|Ruby}}==
Line 983:
do critical stuff
end</lang>
=={{header|Rust}}==
Rust's standard library provides <tt>std::sync::Mutex</tt>. Locking the mutex
returns a guard that allows accessing the shared data exclusively. When the
guard goes out of its scope (and is dropped), the mutex gets unlocked again.
Following small program demonstrates using the mutex with two threads that
append to a shared string.
<lang rust>use std::{
sync::{Arc, Mutex},
thread,
time::Duration,
};
fn main() {
let shared = Arc::new(Mutex::new(String::new()));
let handle1 = {
let value = shared.clone();
thread::spawn(move || {
for _ in 0..20 {
thread::sleep(Duration::from_millis(200));
// The guard is valid until the end of the block
let mut guard = value.lock().unwrap();
guard.push_str("A");
println!("{}", guard);
}
})
};
let handle2 = {
let value = shared.clone();
thread::spawn(move || {
for _ in 0..20 {
thread::sleep(Duration::from_millis(300));
{
// Making the guard scope explicit here
let mut guard = value.lock().unwrap();
guard.push_str("B");
println!("{}", guard);
}
}
})
};
handle1.join().ok();
handle2.join().ok();
shared.lock().ok().map_or((), |it| println!("Done: {}", it));
}
</lang>
=={{header|Tcl}}==
|