1 个稳定版本
| 1.0.0 | 2023年2月24日 |
|---|
431 在 内存管理
885 每月下载量
5KB
arc-atomic-ref
一个 AtomicRef 是一种智能指针类型,可以与许多不同的执行线程共享,同时可以原子地与新数据交换出来。这样,当你可以替换包含的数据而不是修改它时,它类似于无锁的 RwLock 或 Mutex。
use arc_atomic_ref::AtomicRef;
use std::sync::Arc;
let ptr = AtomicRef::new(1);
// share ptr with many threads with `clone`
// change its contained value, requires a new `Arc`
ptr.swap(Arc::new(2));
// all threads should see the change, use `.load()` to get the value
assert_eq!(**ptr.load(), 2);
lib.rs:
arc-atomic-ref
一个 AtomicRef 是一种智能指针类型,可以与许多不同的执行线程共享,同时可以原子地与新数据交换出来。这样,当你可以替换包含的数据而不是修改它时,它类似于无锁的 RwLock 或 Mutex。
use arc_atomic_ref::AtomicRef;
use std::sync::Arc;
let ptr = AtomicRef::new(1);
// share ptr with many threads with `clone`
// change its contained value, requires a new `Arc`
ptr.swap(Arc::new(2));
// all threads should see the change, use `.load()` to get the value
assert_eq!(**ptr.load(), 2);
依赖
~185KB