From efa3a22cb9bc78b0744f748b41252ef4ce1b96a9 Mon Sep 17 00:00:00 2001 From: Botahamec Date: Mon, 27 May 2024 01:00:25 -0400 Subject: fix race condition in example --- examples/fibonacci.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/fibonacci.rs b/examples/fibonacci.rs index d43b01c..869ef71 100644 --- a/examples/fibonacci.rs +++ b/examples/fibonacci.rs @@ -1,4 +1,4 @@ -use happylock::{LockCollection, Mutex, ThreadKey}; +use happylock::{collection, LockCollection, Mutex, ThreadKey}; use std::thread; const N: usize = 36; @@ -6,19 +6,25 @@ const N: usize = 36; static DATA: [Mutex; 2] = [Mutex::new(0), Mutex::new(1)]; fn main() { + let mut threads = Vec::new(); for _ in 0..N { - thread::spawn(move || { + let th = thread::spawn(move || { let key = ThreadKey::get().unwrap(); // a reference to a type that implements `OwnedLockable` will never // contain duplicates, so no duplicate checking is needed. - let collection = LockCollection::new_ref(&DATA); + let collection = collection::RetryingLockCollection::new_ref(&DATA); let mut guard = collection.lock(key); let x = *guard[1]; *guard[1] += *guard[0]; *guard[0] = x; }); + threads.push(th); + } + + for thread in threads { + _ = thread.join(); } let key = ThreadKey::get().unwrap(); -- cgit v1.2.3