diff options
| author | Botahamec <botahamec@outlook.com> | 2024-03-06 22:40:20 -0500 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2024-03-06 22:40:20 -0500 |
| commit | 8311c58b99aa86f4a971ea208e1fb3a9a825d566 (patch) | |
| tree | e30016adea76308c3017e61ca7fe5ddeb5aace2c /examples/basic.rs | |
| parent | d96edbd12da892a101362ae89fb3c10917361fe6 (diff) | |
Added some examples
Diffstat (limited to 'examples/basic.rs')
| -rw-r--r-- | examples/basic.rs | 22 |
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); +} |
