summaryrefslogtreecommitdiff
path: root/examples/basic.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/basic.rs')
-rw-r--r--examples/basic.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/examples/basic.rs b/examples/basic.rs
new file mode 100644
index 0000000..535b80a
--- /dev/null
+++ b/examples/basic.rs
@@ -0,0 +1,22 @@
+use std::thread;
+
+use happylock::mutex::Mutex;
+use happylock::ThreadKey;
+
+const N: usize = 10;
+
+static DATA: Mutex<i32> = Mutex::new(0);
+
+fn main() {
+ for _ in 0..N {
+ thread::spawn(move || {
+ let key = ThreadKey::lock().unwrap();
+ let mut data = DATA.lock(key);
+ *data += 1;
+ });
+ }
+
+ let key = ThreadKey::lock().unwrap();
+ let data = DATA.lock(key);
+ println!("{}", *data);
+}