On this page
  1. Mutex
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

Mutex

Prev: sync | Up: sync | Next: `RwLock`
Rel: sync, rust/builtins
Ref: Rust std — Mutex
Tags: #public #prog #rust #stdlib #sync #tier/core


Signature & Overview

Mutex<T>

Mutual-exclusion lock that owns the protected data T — impossible to access T without locking.

Notes & Pitfalls

Example

use std::sync::{Arc, Mutex};
use std::thread;

fn main() {
    let total = Arc::new(Mutex::new(0));
    let t = Arc::clone(&total);
    thread::spawn(move || { *t.lock().unwrap() += 10; }).join().unwrap();
    assert_eq!(*total.lock().unwrap(), 10);
}

Linked from

Related notes