Back to course

Shared State: Mutex & Arc

Rust for Systems & Web3 Security

Thread-Safe Shared State

To share state across threads, you need Arc (Atomically Reference Counted) and Mutex (Mutual Exclusion).

  • Arc: Allows multiple threads to own the pointer.
  • Mutex: Ensures only one thread can access the data at a time.

rust let counter = Arc::new(Mutex::new(0));