From d755c0c1f853362976c3064139646a7f621276ec Mon Sep 17 00:00:00 2001 From: ddidderr Date: Thu, 10 Mar 2022 16:32:43 +0100 Subject: [PATCH] RwLock: 16 reader threads and 1 writer thread Just explored a bit the locking mechanism of std::sync::RwLock. --- .gitignore | 1 + Cargo.lock | 14 ++++++++++ Cargo.toml | 9 +++++++ src/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5fdf230 --- /dev/null +++ b/Cargo.lock @@ -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", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2464621 --- /dev/null +++ b/Cargo.toml @@ -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" } diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bf864a7 --- /dev/null +++ b/src/main.rs @@ -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(); +}