diff options
| author | Mica White <botahamec@outlook.com> | 2024-03-08 11:45:15 -0500 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2024-03-08 11:45:15 -0500 |
| commit | 6b4951ade670acbe3cb34b2002fbcd4b4e6a7300 (patch) | |
| tree | c800e9888ba649e2cff27cc1b8ae001c3632572d /examples/double_mutex.rs | |
| parent | cff337867884fc5d9eff80c77d41e64ee53c6115 (diff) | |
Replace ownership with mutable access
Diffstat (limited to 'examples/double_mutex.rs')
| -rw-r--r-- | examples/double_mutex.rs | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/examples/double_mutex.rs b/examples/double_mutex.rs index 76f3294..033bfed 100644 --- a/examples/double_mutex.rs +++ b/examples/double_mutex.rs @@ -1,27 +1,27 @@ use std::thread; -use happylock::mutex::Mutex; +use happylock::mutex::{Mutex, SpinLock}; use happylock::{LockGuard, ThreadKey}; const N: usize = 10; -static DATA_1: Mutex<i32> = Mutex::new(0); -static DATA_2: Mutex<String> = Mutex::new(String::new()); +static DATA_1: SpinLock<i32> = Mutex::new(0); +static DATA_2: SpinLock<String> = Mutex::new(String::new()); fn main() { for _ in 0..N { thread::spawn(move || { - let key = ThreadKey::lock().unwrap(); + let mut key = ThreadKey::lock().unwrap(); let data = (&DATA_1, &DATA_2); - let mut guard = LockGuard::lock(&data, key); + let mut guard = LockGuard::lock(&data, &mut key); *guard.1 = (100 - *guard.0).to_string(); *guard.0 += 1; }); } - let key = ThreadKey::lock().unwrap(); + let mut key = ThreadKey::lock().unwrap(); let data = (&DATA_1, &DATA_2); - let data = LockGuard::lock(&data, key); + let data = LockGuard::lock(&data, &mut key); println!("{}", *data.0); println!("{}", *data.1); } |
