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 | |
| parent | 8ea16a606bfcc1ba535f6cef3cb4c162f91d2eb0 (diff) | |
Pointer checks
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/double_mutex.rs | 8 | ||||
| -rw-r--r-- | examples/list.rs | 32 |
2 files changed, 25 insertions, 15 deletions
diff --git a/examples/double_mutex.rs b/examples/double_mutex.rs index d1a939c..df11b7a 100644 --- a/examples/double_mutex.rs +++ b/examples/double_mutex.rs @@ -1,6 +1,6 @@ use std::thread; -use happylock::{LockGuard, Mutex, ThreadKey}; +use happylock::{LockCollection, Mutex, ThreadKey}; const N: usize = 10; @@ -13,7 +13,8 @@ fn main() { let th = thread::spawn(move || { let key = ThreadKey::lock().unwrap(); let data = (&DATA_1, &DATA_2); - let mut guard = LockGuard::lock(&data, key); + let lock = LockCollection::new(data).unwrap(); + let mut guard = lock.lock(key); *guard.1 = (100 - *guard.0).to_string(); *guard.0 += 1; }); @@ -26,7 +27,8 @@ fn main() { let key = ThreadKey::lock().unwrap(); let data = (&DATA_1, &DATA_2); - let data = LockGuard::lock(&data, key); + let data = LockCollection::new(data).unwrap(); + let data = data.lock(key); println!("{}", *data.0); println!("{}", *data.1); } 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); } |
