Sync is a #202204061235 that allows the type to be referenced in a multithreading environment#. Rust primitive types has this trait implemented by default, and same as 202207172159, if the type is entirely composed of these types, then it will have this trait implicitly. Some types such as raw pointers, #202206221720 and Cell<T>
related types (including #202206232210) doesn’t implement this trait.
Rust Sync
- Rust Trait
-
Rust RefCell
However, it can’t be used in a multithreading environment# as it doesn’t implement the 202204061235# 202207172210#.
-
Reference Counting Pointer
Rc<T>
can’t be used in a multithreading environment# as it doesn’t implement the 202204061235# 202207172159# and 202207172210#. -
Mutex in Rust
As you can see that, we wrap around the Mutex into another type,
Arc
(imported fromstd::sync::Arc
). It allows the data to be shared across multiple threads without violating the rule of Rust ownership, that is, it is atomic. You can treat it as 202206221720# with the 202207172159# and 202207172210# 202204061235#.Further inspection into the code, we find out that Mutex offer interior mutability, and can be 202206221604# using the syntax
*
. It is essentially the same to the 202206232210#. Therefore, one advice is to treat Mutex as if it is a 202111301656# with 202207172210# 202204061235#.