Arc
Arc<T>
allows shared, read-only ownership via Arc::clone
:
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.
Arc
代表“原子引用计数”,它是使用原子操作的Rc
的 线程安全版本。Arc<T>
implementsClone
whether or notT
does. It implementsSend
andSync
if and only ifT
implements them both.Arc::clone()
在执行原子操作方面有开销,但在此之后,T
便可 随意使用,而没有任何开销。- 请警惕引用循环,
Arc
不会使用垃圾回收器检测引用循环。std::sync::Weak
对此有所帮助。