2 个版本
0.1.1 | 2023年2月24日 |
---|---|
0.1.0 | 2023年2月8日 |
#827 在 并发
81KB
1K SLoC
SwapArc
您可以通过复制以下行将 SwapArc 添加到您的 Cargo toml 中:swap-arc = "0.1.0"
注意:这基本上是一个更快的(仅在多个 x86_64 系统上测试过)的替换方案,用于 ArcSwap
SwapArc 允许您在使用 Arc 的同时将其交换出来。
让我们来看一个例子
use std::sync::Arc;
use std::thread;
struct Config {
timout: u64,
...
}
struct Server {
config: SwapArc<Config>,
...
}
fn test() {
let server = Arc::new(Server {
config: Config {
timout: 1000,
...
},
...
});
thread::spawn(|| {
loop {
// load the config without fearing any blocking or expensive operations.
server.accept_connection(server.config.load().timeout);
}
});
...
}
// on network update, update the config seamlessly without blocking loads
server.config.update(Arc::new(Config {
timeout: ...,
...
}));
依赖项
~180–420KB