In Rust, we can use a shared memory Concurrency model by means of concurrent data structures and traditional locking mechanisms such as mutexes.
Mutex<T>
is a smart pointer that provides mutual exclusion access to its inner value. To access a mutable reference to the inner value, we have to acquire the lock and then operate on a MutexGuard
value, which can be dereferenced to the actual inner value. The lock from Mutex<T>
is automatically released when the MutexGuard
value is dropped.
To share a Mutex<T>
across threads, we have to use an atomically referenced counted Arc<T>
smart pointer. With this, we can safely count the references for the Mutex<T>
across threads and ensure its proper cleanup.