Cargo.toml:
x
[dependencies]
flume = "0.10.13"
src/main.rs:
use flume;
use std::thread;
use std::thread::sleep;
use std::time::Duration;
fn main() {
let (tx, rx) = flume::bounded::<i32>(3);
let recv_thread = thread::spawn(move || {
let rx = rx.clone();
let mut received_data: i32 = 0;
while received_data != 4 {
received_data = rx.recv().unwrap();
println!("received {}", received_data);
sleep(Duration::from_secs(1))
}
});
for n in 1..5 {
println!("send {}", n);
tx.send(n).unwrap();
}
recv_thread.join().unwrap();
}
输出:
xxxxxxxxxx
send 1
send 2
received 1
send 3
send 4
received 2
received 3
received 4