3 个版本
0.1.2 | 2022年3月26日 |
---|---|
0.1.1 | 2022年3月26日 |
0.1.0 | 2022年3月26日 |
#635 in 并发
每月 22 次下载
用于 yuxii
20KB
334 行
go-spawn
go-spawn
是一个提供宏以最小化样板代码来启动和连接线程的库。线程可以用 go!
或 go_ref!
启动,可以用 join!
或 join_all!
连接。
安装
go-spawn
发布在 https://crates.io
。要在 Rust 项目中使用,请将 go-spawn
的最新版本添加到您的 Cargo.toml
文件中
[dependencies]
go-spawn = "0.1.0"
示例。
use go_spawn::{go, join};
use std::sync::{
atomic::{AtomicI64, Ordering},
Arc,
};
let counter = Arc::new(AtomicI64::new(0));
let counter_cloned = counter.clone();
// Spawn a thread that captures values by move.
go! {
for _ in 0..100 {
counter_cloned.fetch_add(1, Ordering::SeqCst));
}
}
// Join the most recent thread spawned by `go_spawn` that has not yet been joined.
assert!(join!().is_ok());
assert_eq!(counter.load(Ordering::SeqCst), 100);
use go_spawn::{go_ref, join};
use std::sync::{
atomic::{AtomicI64, Ordering},
};
static COUNTER: AtomicI64 = AtomicI64::new(0);
for _ in 0..10 {
// Spawn a thread that captures by reference.
go_ref!(COUNTER.fetch_add(1, Ordering::SeqCst));
}
// Join all not-yet-joined threads spawned by `go_spawn`.
join_all!();
assert_eq!(counter.load(Ordering::SeqCst), 10);
文档
文档可在 https://docs.rs/go-spawn
找到。
依赖
~0.4–27MB
~350K SLoC