Browse Source

Simple test for RwLock

master
Julio Biason 3 years ago
parent
commit
f4ddfbb2aa
  1. 7
      rwlocktest/Cargo.lock
  2. 8
      rwlocktest/Cargo.toml
  3. 31
      rwlocktest/src/main.rs

7
rwlocktest/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rwlocktest"
version = "0.1.0"

8
rwlocktest/Cargo.toml

@ -0,0 +1,8 @@
[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]

31
rwlocktest/src/main.rs

@ -0,0 +1,31 @@
use std::sync::RwLock;
struct Holder {
content: RwLock<String>,
}
impl Holder {
fn new() -> Self {
Self {
content: RwLock::new(String::from("hello")),
}
}
fn change(&self, content: &str) {
let mut original = self.content.write().unwrap();
*original = content.into();
}
fn content(&self) -> String {
let lock = self.content.read().unwrap();
String::from(&*lock)
}
}
fn main() {
let content = Holder::new();
println!("Content: {}", content.content());
content.change("Hello there");
println!("Content: {}", content.content());
}
Loading…
Cancel
Save