summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2024-05-22 20:47:28 -0400
committerBotahamec <botahamec@outlook.com>2024-05-22 20:47:28 -0400
commit1ed88daa00d478472181f0987112a2b0f2266694 (patch)
tree877f85d1fc70f3e402c7d62f33fe2259c05ad68f /examples
parentef191a3e8ecf4093fcd08036e35012c1af173a08 (diff)
top-level docs
Diffstat (limited to 'examples')
-rw-r--r--examples/fibonacci.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/examples/fibonacci.rs b/examples/fibonacci.rs
new file mode 100644
index 0000000..d43b01c
--- /dev/null
+++ b/examples/fibonacci.rs
@@ -0,0 +1,29 @@
+use happylock::{LockCollection, Mutex, ThreadKey};
+use std::thread;
+
+const N: usize = 36;
+
+static DATA: [Mutex<i32>; 2] = [Mutex::new(0), Mutex::new(1)];
+
+fn main() {
+ for _ in 0..N {
+ 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 mut guard = collection.lock(key);
+
+ let x = *guard[1];
+ *guard[1] += *guard[0];
+ *guard[0] = x;
+ });
+ }
+
+ let key = ThreadKey::get().unwrap();
+ let data = LockCollection::new_ref(&DATA);
+ let data = data.lock(key);
+ println!("{}", data[0]);
+ println!("{}", data[1]);
+}