diff options
| author | Mica White <botahamec@outlook.com> | 2024-03-09 16:53:12 -0500 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2024-03-09 16:53:12 -0500 |
| commit | 6a54884b292987fc1371bf062c42e964b6a4b0fe (patch) | |
| tree | 7b4c20c4150522b0d6d8e3f5db8fbe17eb80abc5 /examples/list.rs | |
| parent | 8ea16a606bfcc1ba535f6cef3cb4c162f91d2eb0 (diff) | |
Pointer checks
Diffstat (limited to 'examples/list.rs')
| -rw-r--r-- | examples/list.rs | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/examples/list.rs b/examples/list.rs index 3903260..368a633 100644 --- a/examples/list.rs +++ b/examples/list.rs @@ -1,6 +1,6 @@ use std::thread; -use happylock::{LockGuard, Mutex, ThreadKey}; +use happylock::{LockCollection, Mutex, ThreadKey}; const N: usize = 10; @@ -30,16 +30,23 @@ fn main() { for _ in 0..N { let th = thread::spawn(move || { let mut key = ThreadKey::lock().unwrap(); - let mut data = Vec::new(); - for _ in 0..3 { - let rand = random(&mut key); - data.push(&DATA[rand % 6]); - } + loop { + let mut data = Vec::new(); + for _ in 0..3 { + let rand = random(&mut key); + data.push(&DATA[rand % 6]); + } + + let Some(lock) = LockCollection::new(data) else { + continue; + }; + let mut guard = lock.lock(&mut key); + *guard[0] += *guard[1]; + *guard[1] += *guard[2]; + *guard[2] += *guard[0]; - let mut guard = LockGuard::lock(&data, key); - *guard[0] += *guard[1]; - *guard[1] += *guard[2]; - *guard[2] += *guard[0]; + return; + } }); threads.push(th); } @@ -48,8 +55,9 @@ fn main() { _ = th.join(); } - let mut key = ThreadKey::lock().unwrap(); - let data = LockGuard::lock(&DATA, &mut key); + let key = ThreadKey::lock().unwrap(); + let data = LockCollection::new(&DATA).unwrap(); + let data = data.lock(key); for val in &*data { println!("{}", **val); } |
