Arc
Arc<T> allows shared, read-only ownership via Arc::clone:
// Copyright 2024 Google LLC
// SPDX-License-Identifier: Apache-2.0
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(WhereDropped(vec![10, 20, 30]));
let mut handles = Vec::new();
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());
}
This slide should take about 5 minutes.
Arcstands for “Atomic Reference Counted”, a thread safe version ofRcthat uses atomic operations.Arc<T>implementsClonewhether or notTdoes. It implementsSendandSyncif and only ifTimplements them both.Arc::clone()has the cost of atomic operations that get executed, but after that the use of theTis free.- Beware of reference cycles,
Arcdoes not use a garbage collector to detect them.std::sync::Weakcan help.