summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/dining_philosophers.rs5
-rw-r--r--examples/double_mutex.rs9
-rw-r--r--examples/list.rs4
3 files changed, 9 insertions, 9 deletions
diff --git a/examples/dining_philosophers.rs b/examples/dining_philosophers.rs
index 9600c8c..f8657df 100644
--- a/examples/dining_philosophers.rs
+++ b/examples/dining_philosophers.rs
@@ -48,7 +48,10 @@ impl Philosopher {
fn cycle(&self) {
let key = ThreadKey::lock().unwrap();
thread::sleep(Duration::from_secs(1));
- let forks = LockCollection::new([&FORKS[self.left], &FORKS[self.right]]).unwrap();
+
+ // safety: no philosopher asks for the same fork twice
+ let forks =
+ unsafe { LockCollection::new_unchecked([&FORKS[self.left], &FORKS[self.right]]) };
let forks = forks.lock(key);
println!("{} is eating...", self.name);
thread::sleep(Duration::from_secs(1));
diff --git a/examples/double_mutex.rs b/examples/double_mutex.rs
index df11b7a..320f461 100644
--- a/examples/double_mutex.rs
+++ b/examples/double_mutex.rs
@@ -4,16 +4,14 @@ use happylock::{LockCollection, Mutex, ThreadKey};
const N: usize = 10;
-static DATA_1: Mutex<i32> = Mutex::new(0);
-static DATA_2: Mutex<String> = Mutex::new(String::new());
+static DATA: (Mutex<i32>, Mutex<String>) = (Mutex::new(0), Mutex::new(String::new()));
fn main() {
let mut threads = Vec::new();
for _ in 0..N {
let th = thread::spawn(move || {
let key = ThreadKey::lock().unwrap();
- let data = (&DATA_1, &DATA_2);
- let lock = LockCollection::new(data).unwrap();
+ let lock = LockCollection::new_ref(&DATA);
let mut guard = lock.lock(key);
*guard.1 = (100 - *guard.0).to_string();
*guard.0 += 1;
@@ -26,8 +24,7 @@ fn main() {
}
let key = ThreadKey::lock().unwrap();
- let data = (&DATA_1, &DATA_2);
- let data = LockCollection::new(data).unwrap();
+ let data = LockCollection::new_ref(&DATA);
let data = data.lock(key);
println!("{}", *data.0);
println!("{}", *data.1);
diff --git a/examples/list.rs b/examples/list.rs
index 368a633..14117ee 100644
--- a/examples/list.rs
+++ b/examples/list.rs
@@ -37,7 +37,7 @@ fn main() {
data.push(&DATA[rand % 6]);
}
- let Some(lock) = LockCollection::new(data) else {
+ let Some(lock) = LockCollection::try_new(data) else {
continue;
};
let mut guard = lock.lock(&mut key);
@@ -56,7 +56,7 @@ fn main() {
}
let key = ThreadKey::lock().unwrap();
- let data = LockCollection::new(&DATA).unwrap();
+ let data = LockCollection::new_ref(&DATA);
let data = data.lock(key);
for val in &*data {
println!("{}", **val);