
Rust Essentials
By :

How can we make the not_shared.rs
program give us the correct result? Rust provides tools, such as atomic types from the submodule std::sync::atomic
, to handle shared mutable state safely. In order to share data, you need to wrap the data in some of these sync primitives, such as Arc
, Mutex
, RwLock
, AtomicUSize
, and so on.
Basically, the principle of locking is used, as in operating systems and database systems. Exclusive access to a resource is given to the thread that has obtained a lock (also called a mutex, from mutually exclusive) on the resource. A lock can only be obtained by one thread at a time. In this way, two threads cannot change this resource at the same time, so no data races can occur; locking atomicity is enforced when required. When the thread that has acquired the lock has done its work, the lock is removed and another thread can then work with the data.
In Rust, this is done with the generic Mutex<T>
type from the std::sync
module. The sync comes...
Change the font size
Change margin width
Change background colour