You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-24 01:36:44 +02:00
Publish Comprehensive Rust 🦀
This commit is contained in:
41
src/concurrency/send-sync/examples.md
Normal file
41
src/concurrency/send-sync/examples.md
Normal file
@ -0,0 +1,41 @@
|
||||
# Examples
|
||||
|
||||
## `Send + Sync`
|
||||
|
||||
Most types you come across are `Send + Sync`:
|
||||
|
||||
* `i8`, `f32`, `bool`, `char`, `&str`, ...
|
||||
* `(T1, T2)`, `[T; N]`, `&[T]`, `struct { x: T }`, ...
|
||||
* `String`, `Option<T>`, `Vec<T>`, `Box<T>`, ...
|
||||
* `Arc<T>`: Explicitly thread-safe via atomic reference count.
|
||||
* `Mutex<T>`: Explicitly thread-safe via internal locking.
|
||||
* `AtomicBool`, `AtomicU8`, ...: Uses special atomic instructions.
|
||||
|
||||
The generic types are typically `Send + Sync` when the type parameters are
|
||||
`Send + Sync`.
|
||||
|
||||
## `Send + !Sync`
|
||||
|
||||
These types can be moved to other threads, but they're not thread-safe.
|
||||
Typically because of interior mutability:
|
||||
|
||||
* `mpsc::Sender<T>`
|
||||
* `mpsc::Receiver<T>`
|
||||
* `Cell<T>`
|
||||
* `RefCell<T>`
|
||||
|
||||
## `!Send + Sync`
|
||||
|
||||
These types are thread-safe, but they cannot be moved to another thread:
|
||||
|
||||
* `MutexGuard<T>`: Uses OS level primitives which must be deallocated on the
|
||||
thread which created them.
|
||||
|
||||
## `!Send + !Sync`
|
||||
|
||||
These types are not thread-safe and cannot be moved to other threads:
|
||||
|
||||
* `Rc<T>`: each `Rc<T>` has a reference to an `RcBox<T>`, which contains a
|
||||
non-atomic reference count.
|
||||
* `*const T`, `*mut T`: Rust that there are special lifetime considerations for the
|
||||
pointer.
|
9
src/concurrency/send-sync/send.md
Normal file
9
src/concurrency/send-sync/send.md
Normal file
@ -0,0 +1,9 @@
|
||||
# `Send`
|
||||
|
||||
> A type `T` is [`Send`][1] if it is safe to move a `T` value to another thread.
|
||||
|
||||
The effect of moving ownership to another thread is that _destructors_ will run
|
||||
in that thread. So the question is when you can allocate a value in one thread
|
||||
and deallocate it in another.
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/marker/trait.Send.html
|
10
src/concurrency/send-sync/sync.md
Normal file
10
src/concurrency/send-sync/sync.md
Normal file
@ -0,0 +1,10 @@
|
||||
# `Sync`
|
||||
|
||||
> A type `T` is [`Sync`][1] if it is safe to access a `T` value from multiple
|
||||
> threads at the same time.
|
||||
|
||||
More precisely, the definitions is
|
||||
|
||||
> `T` is `Sync` if and only if `&T` is `Send`
|
||||
|
||||
[1]: https://doc.rust-lang.org/std/marker/trait.Sync.html
|
Reference in New Issue
Block a user