Example

Let us see Arc and Mutex in action:

  1. use std::thread;
  2. // use std::sync::{Arc, Mutex};
  3. fn main() {
  4.     let v = vec![10, 20, 30];
  5.     let handle = thread::spawn(|| {
  6.         v.push(10);
  7.     });
  8.     v.push(1000);
  9.     handle.join().unwrap();
  10.     println!("v: {v:?}");
  11. }

This slide should take about 8 minutes.

Possible solution:

  1. use std::sync::{Arc, Mutex};
  2. use std::thread;
  3. fn main() {
  4.     let v = Arc::new(Mutex::new(vec![10, 20, 30]));
  5.     let v2 = Arc::clone(&v);
  6.     let handle = thread::spawn(move || {
  7.         let mut v2 = v2.lock().unwrap();
  8.         v2.push(10);
  9.     });
  10.     {
  11.         let mut v = v.lock().unwrap();
  12.         v.push(1000);
  13.     }
  14.     handle.join().unwrap();
  15.     println!("v: {v:?}");
  16. }

Notable parts:

  • v is wrapped in both Arc and Mutex, because their concerns are orthogonal.
    • Wrapping a Mutex in an Arc is a common pattern to share mutable state between threads.
  • v: Arc<_> needs to be cloned as v2 before it can be moved into another thread. Note move was added to the lambda signature.
  • Blocks are introduced to narrow the scope of the LockGuard as much as possible.