From 6e94e27e06d172f149a3810fd9d51981f0b0f733 Mon Sep 17 00:00:00 2001 From: darkicewolf50 Date: Mon, 17 Mar 2025 15:54:52 -0600 Subject: [PATCH] finished ch16.4 and ch16 --- .obsidian/workspace.json | 20 ++++++++-- Async, Await, Futures and Streams.md | 1 + Concurrency.md | 2 +- Sync and Send.md | 56 ++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 Async, Await, Futures and Streams.md diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 933e60a..a02fb00 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -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", diff --git a/Async, Await, Futures and Streams.md b/Async, Await, Futures and Streams.md new file mode 100644 index 0000000..9bc2efb --- /dev/null +++ b/Async, Await, Futures and Streams.md @@ -0,0 +1 @@ +# Fundamentals of Asynchronous Programming: Async, Await, Futures, and Streams diff --git a/Concurrency.md b/Concurrency.md index 546b880..2f01428 100644 --- a/Concurrency.md +++ b/Concurrency.md @@ -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) \ No newline at end of file +- 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) diff --git a/Sync and Send.md b/Sync and Send.md index 2e6dc0f..7d573c4 100644 --- a/Sync and Send.md +++ b/Sync and Send.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`. + +This cannot be be `Send` because if you cloned a `Rc` 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` 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` value across threads unsafely. + +When we tried to do this before we got the error: the trait `Send` is not implemented for `Rc>`. + +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` is not `Sync` as well for the same reasons that it is not `Send`. + +The `RefCell` types ([Ch15](./Smart%20Pointers.md)) and the family related to `Cell` types are not `Sync`. + +The implementation for borrow checking that `RefCell` does at runtime is not thread-safe. + + +The smart pointer `Mutex` is `Sync` and can be used to share access with multiple threads alike what we saw in ["Sharing a `Mutex` 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. \ No newline at end of file