mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-06-15 04:54:17 -06:00
finished ch16.4 and ch16
This commit is contained in:
parent
084ac0ecff
commit
6e94e27e06
20
.obsidian/workspace.json
vendored
20
.obsidian/workspace.json
vendored
@ -35,6 +35,20 @@
|
||||
"title": "Concurrency"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "42d65c7d3f15198d",
|
||||
"type": "leaf",
|
||||
"state": {
|
||||
"type": "markdown",
|
||||
"state": {
|
||||
"file": "Async, Await, Futures and Streams.md",
|
||||
"mode": "source",
|
||||
"source": false
|
||||
},
|
||||
"icon": "lucide-file",
|
||||
"title": "Async, Await, Futures and Streams"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "2a974ca5442d705f",
|
||||
"type": "leaf",
|
||||
@ -231,11 +245,12 @@
|
||||
"command-palette:Open command palette": false
|
||||
}
|
||||
},
|
||||
"active": "2a974ca5442d705f",
|
||||
"active": "42d65c7d3f15198d",
|
||||
"lastOpenFiles": [
|
||||
"Concurrency.md",
|
||||
"Sync and Send.md",
|
||||
"Async, Await, Futures and Streams.md",
|
||||
"Shared State Concurrency.md",
|
||||
"Sync and Send.md",
|
||||
"().md",
|
||||
"Smart Pointers.md",
|
||||
"Simultaneous Code Running.md",
|
||||
@ -259,7 +274,6 @@
|
||||
"Generic Types Traits and Lifetimes.md",
|
||||
"Generics.md",
|
||||
"Lifetimes.md",
|
||||
"2025-02-04.md",
|
||||
"does_not_compile.svg",
|
||||
"Untitled.canvas",
|
||||
"Good and Bad Code/Commenting Pratices",
|
||||
|
1
Async, Await, Futures and Streams.md
Normal file
1
Async, Await, Futures and Streams.md
Normal file
@ -0,0 +1 @@
|
||||
# Fundamentals of Asynchronous Programming: Async, Await, Futures, and Streams
|
@ -46,4 +46,4 @@ Here are the topics that will be covered in this section:
|
||||
- How to create threads to run multiple pieces of code at the same time [Section Link Here](./Simultaneous%20Code%20Running.md)
|
||||
- *Message-passing* concurrency, where channels send messages between threads [Section Link Here](./Passing%20Data%20Between%20Threads.md)
|
||||
- *Shared-state* concurrency, where multiple threads have access to some piece of data [Section Link Here](./Shared%20State%20Concurrency.md)
|
||||
- The `Sync` and `Send` traits, which extend Rust's concurrency guarantees to use-defined types as well as types provided by the std library [Section Link Here](./Sync%20and%20Send.md)
|
||||
- The `Sync` and `Send` traits, which extend Rust's concurrency guarantees to use-defined types as well as types provided by the std library [Section Link Here](./Sync%20and%20Send.md)
|
||||
|
@ -1 +1,57 @@
|
||||
# Extensible Concurrency with the `Sync` and `Send` Traits
|
||||
The Rust language gas *very* few concurrency features.
|
||||
|
||||
Almost every feature we talked about so far has been part of the std library and not the language.
|
||||
|
||||
Your options for handling concurrency are not limited to the language or the std library; you are able to write your own concurrency features or use those written by others.
|
||||
|
||||
However, tow concurrency concepts that are embedded into the language are the `std::marker` traits `Sync` and `Send`.
|
||||
|
||||
## Allowing Transference of Ownership Between Threads with `Send`
|
||||
The marker trait `Send` indicates that ownership of values of the type implemented `Send` can be transferred between threads.
|
||||
|
||||
Almost every Rust type is `Send`.
|
||||
|
||||
There are some exceptions, including `Rc<T>`.
|
||||
|
||||
This cannot be be `Send` because if you cloned a `Rc<T>` value and tried to transfer ownership of the clone to another thread, both threads might update the reference count at the same time.
|
||||
|
||||
This is the reason, `Rc<T>` is implemented for use in single-threaded situations where you don't want to pay the thread-safe performance penalty.
|
||||
|
||||
Rust's type system and trait bounds ensure that you can never accidentally send a `Rc<T>` value across threads unsafely.
|
||||
|
||||
When we tried to do this before we got the error: the trait `Send` is not implemented for `Rc<Mutex<i32>>`.
|
||||
|
||||
Any type composed entirely of `Send` types is automatically marked as `Send` as well.
|
||||
|
||||
Almost all primitives are `Send`, aside form raw pointers (This will be discussed in ch20).
|
||||
|
||||
## Allowing access form Multiple Threads with `Sync`
|
||||
|
||||
The `Sync` marker trait indicates that it is safe for the type implementing `Sync` to be referenced from multiple threads.
|
||||
|
||||
Any type `T` is `Sync` if `&T` (an immutable reference to `T`) is `Send`, meaning the reference can be sent safely to another thread.
|
||||
|
||||
Similar to `Send`, primitive types are `Sync` and types composed entirely of types that are `Sync` are also `Sync`.
|
||||
|
||||
The smart pointer `Rc<T>` is not `Sync` as well for the same reasons that it is not `Send`.
|
||||
|
||||
The `RefCell<T>` types ([Ch15](./Smart%20Pointers.md)) and the family related to `Cell<T>` types are not `Sync`.
|
||||
|
||||
The implementation for borrow checking that `RefCell<T>` does at runtime is not thread-safe.
|
||||
|
||||
|
||||
The smart pointer `Mutex<T>` is `Sync` and can be used to share access with multiple threads alike what we saw in ["Sharing a `Mutex<T>` Between Multiple Threads"](./Shared%20State%20Concurrency.md) Section.
|
||||
|
||||
## Implementing `Send` and `Sync` Manually is Unsafe
|
||||
Because types that are made up of `Send` and `Sync` traits are also automatically `Send` and `Sync`, we don't need to implement these traits manually.
|
||||
|
||||
As marker traits they don't even have any methods to implement.
|
||||
|
||||
These are just useful for enforcing invariants related to concurrency.
|
||||
|
||||
Manually implemented these traits involves implementing unsafe Rust code (This will be discussed in Ch20).
|
||||
|
||||
For now the important information is that building new concurrent types not made up of `Send` and `Sync` parts requires careful thought to uphold the safety guarantees.
|
||||
|
||||
["The Rustonomicion"](https://doc.rust-lang.org/nomicon/index.html) has more information about these guarantees and how to uphold them.
|
Loading…
x
Reference in New Issue
Block a user