You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-16 14:17:34 +02:00
concurrency: demonstrate dynamic ownership with Arc
Ownership is the responsibility to free a value. Previously, `v` was always freed by the main thread because it kept the original `Arc` instance. It was difficult to explain why this example did not simply use scoped threads instead. Now we see that multi-threaded dynamic ownership with `Arc` results in a dynamic decision of which thread will clean up a given value.
This commit is contained in:
@ -4,25 +4,40 @@ minutes: 5
|
||||
|
||||
# `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
|
||||
use std::sync::Arc;
|
||||
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() {
|
||||
let v = Arc::new(vec![10, 20, 30]);
|
||||
let v = Arc::new(WhereDropped(vec![10, 20, 30]));
|
||||
let mut handles = Vec::new();
|
||||
for _ in 0..5 {
|
||||
for i in 0..5 {
|
||||
let v = Arc::clone(&v);
|
||||
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();
|
||||
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());
|
||||
println!("v: {v:?}");
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user