about
05/19/2022
RustBites - Synchronization
Rust Bite - Synchronization
safe sharing with Mutex, RwLock, Condvar, Atomic, Arc, Barrier
1.0 Introduction
Table 1. - Common Synchronization Types
| Type | Description |
|---|---|
| Mutex<T> | A mutual exclusion primitive useful for protecting shared data. |
| RwLock<T> | A reader-writer lock allowing multiple readers or at most one writer at any point in time. |
| Condvar | A Condition Variable blocks threads waiting on an event to occur. |
| Atomic | Atomic types provide primitive shared-memory communication between threads. |
| Arc<T> | A thread-safe reference-counting pointer. Arc is used to enable sharing of guarded resources in programs with multiple threads. It holds its data in the heap. |
| Barrier | A barrier enables multiple threads to synchronize the beginning of some computation. |
2.0 Mutex<T>
Table 2. - Mutex Methods:
| Method | Description |
|---|---|
|
|
Creates a new mutex in an unlocked state |
|
|
Acquires a mutex, blocking the current thread until it is able to do so. The returned
|
|
|
If the lock could not be acquired at this time, then Err is returned. Otherwise, an RAII guard is returned. The lock will be unlocked when the guard is dropped. |
|
|
Consumes this mutex, returning the underlying data |
| more methods ... | methods, descriptions, and examples |
Mutex Example
3.0 RwLock<T>
Table 3. - RwLock<T> Methods:
| Method | Description |
|---|---|
|
Creates a new instance of an RwLock |
|
| Locks rwlock with shared read access, blocking current thread until it can be acquired. | |
| Locks rwlock with exclusive write access, blocking current thread until it can be acquired. | |
| Consumes RwLock, returning underlying data. | |
| more methods ... | methods, descriptions, and examples |
RwLock Example
4.0 Condvar
Table 4. - Condvar Methods:
| Methods | Description |
|---|---|
| Creates new condition variable ready to be waited on and notified. | |
|
-> LockResult<MutexGuard<'a, T>> |
Blocks current thread until condition variable receives a notification. |
| Wakes up one blocked thread on this condvar. | |
| Wakes up all blocked threads on this condvar. | |
| more methods ... | methods, descriptions, and examples |
Condvar Example
5.0 Atomics
Table 5. - AtomicBool and AtomicUsize Methods
| Method | Description |
|---|---|
|
|
Ordering: Acquire, Relaxed, SeqCst |
| Creates a new AtomicBool. | |
| Loads a value from the bool. | |
| Stores a value into its bool. | |
| Stores value into its bool, returning previous value. | |
| Consumes atomic and returns contained value. | |
| more methods ... | methods, descriptions, and examples |
| AtomicUsize | Ordering: Acquire, Relaxed, SeqCst |
| Creates a new atomic integer. | |
| Loads value from atomic integer. | |
| Stores value into atomic integer. | |
| Adds to the current value, returning the previous value. | |
| Consumes atomic and returns contained value. | |
| more methods ... | methods, descriptions, and examples |
| more types ... | AtomicI8, ... |
AtomicUsize Example
6.0 Barrier
Table 6. - Barrier Methods
| Method | Description |
|---|---|
| Creates a new barrier that can block a given number of threads. | |
| Blocks the current thread until all threads have rendezvoused here. | |