最简单的使用方式
use std::thread;
let child = thread::spawn(move || {
// 逻辑
});
let res = child.join()
工具函数
- thread::sleep(dur: Duration)
- thread::yield_now()
- thread::park() 暂停线程
- thread::Thread::unparK() 恢复线程
示例
use std::thread;
use std::time::Duration;
let t = thread::Builder::new()
.name("child")
.spawn( move || {
println!("enter child thread");
thread::park()
}).unwrap();
thread::sleep(Duration::new(5,0));
t.thread().unpark();
t.join();
mpsc(即std::sync::mpsc
,意为multi-producer, single-consumer FIFO queue)
代码示例
use std::thread;
use std::sync::mpsc::channel;
let (tx, rx) = channel();
thread::spawn(move || {
for i in 0..10 {
tx.send(i).unwrap();
}
});
while let Ok(r) = rx.recv() {
println!("received {}", r);
}
当然,我们可以在tx之上调用clone()
方法。
同步管道和异步管道在接收端是一样的逻辑,区别在于发送端。
use std::thread;
use std::sync::mpsc::sync_channel;
let (tx, rx) = sync_channel();
tx.send(1).unwrap();
thread::spawn(move || {
tx.send(2).unwrap();
});
println!("received {}", rx.recv().unwrap());