Concurrency Building Blocks
Arc Shared Label
Count Shared Owners
Arc lets multiple owners share the same value. Cloning the Arc adds owners without cloning the inner data.
Program
Play the program to choose how many worker handles share one label.
arc_shared_label.rs
use std::sync::Arc;
fn main() {
let workers = ;
let label = Arc::new("cache");
let handles: Vec<_> = (0..workers).map(|_| Arc::clone(&label)).collect();
println!("workers={} strong={}", handles.len(), Arc::strong_count(&label));
}
use std::sync::Arc;
fn main() {
let workers = ;
let label = Arc::new("cache");
let handles: Vec<_> = (0..workers).map(|_| Arc::clone(&label)).collect();
println!("workers={} strong={}", handles.len(), Arc::strong_count(&label));
}
use std::sync::Arc;
fn main() {
let workers = ;
let label = Arc::new("cache");
let handles: Vec<_> = (0..workers).map(|_| Arc::clone(&label)).collect();
println!("workers={} strong={}", handles.len(), Arc::strong_count(&label));
}
Arc
`Arc` is an atomically reference-counted pointer for shared ownership.
clone
`Arc::clone` creates another owner of the same inner value.
strong_count
`strong_count` reports how many strong owners currently exist.