summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2024-05-27 01:00:25 -0400
committerBotahamec <botahamec@outlook.com>2024-05-27 01:00:25 -0400
commitefa3a22cb9bc78b0744f748b41252ef4ce1b96a9 (patch)
treea3469e1518461c96f690b98e95c01228537d5aba /examples
parentf5f3daee8c15bbd103ea3587edfee9dbd4c26dcc (diff)
fix race condition in example
Diffstat (limited to 'examples')
-rw-r--r--examples/fibonacci.rs12
1 files 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<i32>; 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();