Skip to content

状态共享

全局变量

atomic

1
2
3
4
5
6
use std::sync::atomic::{AtomicUsize, Ordering};

static GLOBAL_THREAD_COUNT: AtomicUsize = AtomicUsize::new(0);

let old_thread_count = GLOBAL_THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
println!("live threads: {}", old_thread_count + 1);

once_cell

https://github.com/matklad/once_cell

lazy_static

https://github.com/rust-lang-nursery/lazy-static.rs