O'Reilly logo

Rust Essentials - Second Edition by Ivo Balbaert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Shared mutable states

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required