1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-05-28 21:27:37 +02:00

Small fixes to concurrency material (#2737)

See individual commits.
This commit is contained in:
Frances Wingerter 2025-05-23 19:54:14 +00:00 committed by GitHub
parent fc6e5c7526
commit c60070bb80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 25 deletions

View File

@ -4,25 +4,40 @@ minutes: 5
# `Arc` # `Arc`
[`Arc<T>`][1] allows shared read-only access via `Arc::clone`: [`Arc<T>`][1] allows shared, read-only ownership via `Arc::clone`:
```rust,editable ```rust,editable
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
/// A struct that prints which thread drops it.
#[derive(Debug)]
struct WhereDropped(Vec<i32>);
impl Drop for WhereDropped {
fn drop(&mut self) {
println!("Dropped by {:?}", thread::current().id())
}
}
fn main() { fn main() {
let v = Arc::new(vec![10, 20, 30]); let v = Arc::new(WhereDropped(vec![10, 20, 30]));
let mut handles = Vec::new(); let mut handles = Vec::new();
for _ in 0..5 { for i in 0..5 {
let v = Arc::clone(&v); let v = Arc::clone(&v);
handles.push(thread::spawn(move || { handles.push(thread::spawn(move || {
// Sleep for 0-500ms.
std::thread::sleep(std::time::Duration::from_millis(500 - i * 100));
let thread_id = thread::current().id(); let thread_id = thread::current().id();
println!("{thread_id:?}: {v:?}"); println!("{thread_id:?}: {v:?}");
})); }));
} }
// Now only the spawned threads will hold clones of `v`.
drop(v);
// When the last spawned thread finishes, it will drop `v`'s contents.
handles.into_iter().for_each(|h| h.join().unwrap()); handles.into_iter().for_each(|h| h.join().unwrap());
println!("v: {v:?}");
} }
``` ```

View File

@ -12,13 +12,15 @@ use std::thread;
fn main() { fn main() {
let v = vec![10, 20, 30]; let v = vec![10, 20, 30];
let handle = thread::spawn(|| { let mut handles = Vec::new();
v.push(10); for i in 0..5 {
}); handles.push(thread::spawn(|| {
v.push(1000); v.push(10 * i);
println!("v: {v:?}");
}));
}
handle.join().unwrap(); handles.into_iter().for_each(|h| h.join().unwrap());
println!("v: {v:?}");
} }
``` ```
@ -32,21 +34,17 @@ use std::thread;
fn main() { fn main() {
let v = Arc::new(Mutex::new(vec![10, 20, 30])); let v = Arc::new(Mutex::new(vec![10, 20, 30]));
let mut handles = Vec::new();
let v2 = Arc::clone(&v); for i in 0..5 {
let handle = thread::spawn(move || { let v = Arc::clone(&v);
let mut v2 = v2.lock().unwrap(); handles.push(thread::spawn(move || {
v2.push(10); let mut v = v.lock().unwrap();
}); v.push(10 * i);
println!("v: {v:?}");
{ }));
let mut v = v.lock().unwrap();
v.push(1000);
} }
handle.join().unwrap(); handles.into_iter().for_each(|h| h.join().unwrap());
println!("v: {v:?}");
} }
``` ```
@ -56,7 +54,7 @@ Notable parts:
orthogonal. orthogonal.
- Wrapping a `Mutex` in an `Arc` is a common pattern to share mutable state - Wrapping a `Mutex` in an `Arc` is a common pattern to share mutable state
between threads. between threads.
- `v: Arc<_>` needs to be cloned as `v2` before it can be moved into another - `v: Arc<_>` needs to be cloned to make a new reference for each new spawned
thread. Note `move` was added to the lambda signature. thread. Note `move` was added to the lambda signature.
- Blocks are introduced to narrow the scope of the `LockGuard` as much as - Blocks are introduced to narrow the scope of the `LockGuard` as much as
possible. possible.

View File

@ -105,11 +105,12 @@ fn spawn_crawler_threads(
result_sender: mpsc::Sender<CrawlResult>, result_sender: mpsc::Sender<CrawlResult>,
thread_count: u32, thread_count: u32,
) { ) {
// To multiplex the non-cloneable Receiver, wrap it in Arc<Mutex<_>>.
let command_receiver = Arc::new(Mutex::new(command_receiver)); let command_receiver = Arc::new(Mutex::new(command_receiver));
for _ in 0..thread_count { for _ in 0..thread_count {
let result_sender = result_sender.clone(); let result_sender = result_sender.clone();
let command_receiver = command_receiver.clone(); let command_receiver = Arc::clone(&command_receiver);
thread::spawn(move || { thread::spawn(move || {
let client = Client::new(); let client = Client::new();
loop { loop {