rust-experiments
Resources
- TheDevMethod: Rust YT tutorials
- Introduction to Rust - Part 15: Futures
- How to use Mutex and Atomically Reference Counted Arc with threads in Rust to share information betw
- How to do concurrency with channels in Rust
Notes
Arc (Atomically Reference Counted)
For thread-safe referencing of a single variable.
- When wanting to share a value between multiple threads, use the
Arc
library.Arc<T>
provides shared ownership of a value of typeT
, allocated in the heap.clone
produces a newArc
instance which points to the same allocation on the heap as the sourceArc
, while increasing a reference count. The only way to mutate through anArc
is to useMutex
,RwLock
or one of theAtomic
types. If you are not sharing reference-counted allocations between threads, consider using Rc for lower overhead (less expensive).
Mutex (Mutual Exclusion Primitive)
For protecting shared data
- Blocks threads waiting for the lock to become available. Data can only be accessed through the RAII guards returned from
lock
andtry_lock
, which guarantees that the data is only ever accessed when the mutex is locked.
Atomic (types)
Primitive shared-memory communication between threads, and are the building blocks of other concurrent types.
- This module defines atomic versions of a select number of primitive types, including
AtomicBool
,AtomicIsize
,AtomicUsize
,AtomicI8
,AtomicU16
, etc. Atomic types present operations that, when used correctly, synchronize updates between threads.