X-Git-Url: https://git.ralfj.de/rust-101.git/blobdiff_plain/5baae0ea037ed642b7fe8975fb3004b29827d5b1..53ea4d72d5db48b56a9cb81397c7302653c74d0b:/workspace/src/part15.rs diff --git a/workspace/src/part15.rs b/workspace/src/part15.rs index 1a6873e..148f648 100644 --- a/workspace/src/part15.rs +++ b/workspace/src/part15.rs @@ -3,6 +3,7 @@ use std::sync::{Arc, Mutex}; use std::thread; +use std::time::Duration; // The derived `Clone` implementation will clone the `Arc`, so all clones will actually talk about the same counter. @@ -36,7 +37,7 @@ pub fn main() { let counter1 = counter.clone(); let handle1 = thread::spawn(move || { for _ in 0..10 { - thread::sleep_ms(15); + thread::sleep(Duration::from_millis(15)); counter1.increment(2); } }); @@ -45,14 +46,14 @@ pub fn main() { let counter2 = counter.clone(); let handle2 = thread::spawn(move || { for _ in 0..10 { - thread::sleep_ms(20); + thread::sleep(Duration::from_millis(20)); counter2.increment(3); } }); // Now we watch the threads working on the counter. for _ in 0..50 { - thread::sleep_ms(5); + thread::sleep(Duration::from_millis(5)); println!("Current value: {}", counter.get()); } @@ -65,7 +66,7 @@ pub fn main() { // **Exercise 15.1**: Add an operation `compare_and_inc(&self, test: usize, by: usize)` that increments the counter by // `by` *only if* the current value is `test`. // -// **Exercise 15.2**: Rather than panicking in case the lock is poisoned, we can use `into_innter` on the error to recover +// **Exercise 15.2**: Rather than panicking in case the lock is poisoned, we can use `into_inner` on the error to recover // the data inside the lock. Change the code above to do that. Try using `unwrap_or_else` for this job.