RwLock: 16 reader threads and 1 writer thread
Just explored a bit the locking mechanism of std::sync::RwLock.
This commit is contained in:
commit
d755c0c1f8
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
14
Cargo.lock
generated
Normal file
14
Cargo.lock
generated
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "pfs-time-measure"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "rwlocktest"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"pfs-time-measure",
|
||||
]
|
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "rwlocktest"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
pfs-time-measure = { path = "../pfs-time-measure" }
|
73
src/main.rs
Normal file
73
src/main.rs
Normal file
@ -0,0 +1,73 @@
|
||||
use std::sync::{Arc,RwLock};
|
||||
use std::time::Duration;
|
||||
|
||||
use pfs_time_measure::Clock;
|
||||
|
||||
const NUM_THREADS:usize = 16;
|
||||
|
||||
|
||||
fn sleep_millis(millis: u64) {
|
||||
std::thread::sleep(Duration::from_millis(millis));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let rwlocked = Arc::new(RwLock::new(42));
|
||||
|
||||
let mut threads = Vec::with_capacity(NUM_THREADS);
|
||||
|
||||
for x in 0..NUM_THREADS {
|
||||
|
||||
let r = rwlocked.clone();
|
||||
|
||||
let t = std::thread::spawn(move|| {
|
||||
let mut clock = Clock::new(format!("t{x} reader_lock"), false);
|
||||
loop {
|
||||
{
|
||||
clock.restart();
|
||||
let rr = r.read().unwrap();
|
||||
clock.stop();
|
||||
clock.print_all_measurements();
|
||||
println!("[READER] acquired lock");
|
||||
println!("[READER] t{x}: {}!", rr);
|
||||
sleep_millis(500);
|
||||
println!("[READER] released lock");
|
||||
}
|
||||
sleep_millis(100);
|
||||
}
|
||||
});
|
||||
threads.push(t);
|
||||
}
|
||||
|
||||
let r = rwlocked.clone();
|
||||
let twrite = std::thread::spawn(move|| {
|
||||
sleep_millis(100);
|
||||
let mut clock = Clock::new("writer_lock".to_string(), false);
|
||||
loop {
|
||||
println!("[WRITER] trying to acquire lock");
|
||||
clock.restart();
|
||||
match r.write() {
|
||||
Ok(mut val) => {
|
||||
clock.stop();
|
||||
clock.print_all_measurements();
|
||||
println!("[WRITER] acquired lock");
|
||||
*val = 11;
|
||||
sleep_millis(600);
|
||||
},
|
||||
Err(e) => {
|
||||
clock.stop();
|
||||
clock.print_all_measurements();
|
||||
println!("[WRITER] error: {:?}", e);
|
||||
},
|
||||
}
|
||||
println!("[WRITER] released lock");
|
||||
sleep_millis(100);
|
||||
}
|
||||
});
|
||||
|
||||
for t in threads {
|
||||
let _ = t.join();
|
||||
}
|
||||
|
||||
let _ = twrite.join();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user