Skip to content

Commit

Permalink
mention atomic primitive types after mutex & rwlock
Browse files Browse the repository at this point in the history
  • Loading branch information
pretzelhammer committed Apr 6, 2021
1 parent b4e7c2d commit 24a8632
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion posts/tour-of-rusts-standard-library-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ unsafe auto trait Sync {}

If a type is `Send` that means it's safe to send between threads. If a type is `Sync` that means it's safe to share references of it between threads. In more precise terms some type `T` is `Sync` if and only if `&T` is `Send`.

Almost all types are `Send` and `Sync`. The only notable `Send` exception is `Rc` and the only notable `Sync` exceptions are `Rc`, `Cell`, `RefCell`. If you need a `Send` version of `Rc` use `Arc`. If you need a `Sync` version of `Cell` or `RefCell` use `Mutex` or `RwLock`.
Almost all types are `Send` and `Sync`. The only notable `Send` exception is `Rc` and the only notable `Sync` exceptions are `Rc`, `Cell`, `RefCell`. If we need a `Send` version of `Rc` we can use `Arc`. If we need a `Sync` version of `Cell` or `RefCell` we can `Mutex` or `RwLock`. Although if we're using the `Mutex` or `RwLock` to just wrap a primitive type it's often better to use the atomic primitive types provided by the standard library such as `AtomicBool`, `AtomicI32`, `AtomicUsize`, and so on.

That almost all types are `Sync` might be a surprise to some people, but yup, it's true even for types without any internal synchronization. This is possible thanks to Rust's strict borrowing rules.

Expand Down

0 comments on commit 24a8632

Please sign in to comment.