use std::sync::mpsc;
use std::thread;
fn main() {
let (s1, r1) = mpsc::channel::<usize>(); // s1 告訴下個執行緒列印 3N+1 , r1 接收 3N+1 的列印數字
let (s2, r2) = mpsc::channel::<usize>(); // s2 告訴下個執行緒列印 3N+2 , r2 接收 3N+2 的列印數字
let (s3, r3) = mpsc::channel::<usize>(); // s3 告訴下個執行緒列印 3N+3 , r3 接收 3N+3 的列印數字
const MAX_PINRT: usize = 100;
thread::spawn(move || {
let id = thread::current().id();
loop {
if let Ok(message) = r2.recv() {
println!("thread {:?} print: {}", id, message);
if message < MAX_PINRT {
s3.send(message + 1).unwrap();
} else {
break; // 列印完成退出
}
} else {
break; // 輸出完成退出
}
}
});
thread::spawn(move || {
let id = thread::current().id();
for val in r3 {
println!("thread {:?} print: {}", id, val);
if val < MAX_PINRT {
s1.send(val + 1).unwrap();
} else {
break;
}
}
});
let id = thread::current().id();
s2.send(1).unwrap();
for val in r1 {
println!("thread {:?} print: {}", id, val);
if val < MAX_PINRT {
s2.send(val + 1).unwrap();
} else {
break;
}
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結