拓冰建站拓冰建站
首页 / 资讯中心 / 正文

rust 学习 多线程1

use std::thread; use std::time::Duration; use std::sync::mpsc; fn main() { let handle thread::spawn(|| { for i in 1..10 { println!({} in spawn thread!,i); thread::sleep(Duration::from_millis(1)); } }); handle.join().unwrap(); //等待上面的完成才进行下面的 for i in 1..5 { println!({} in main thread!,i); thread::sleep(Duration::from_millis(1)); } println!(Hello, World); // handle.join().unwrap(); //等待所有完成 let (tx,rx) mpsc::channel(); //声明一个通道 thread::spawn(move || { let val String::from(hi); tx.send(val).unwrap(); }); let re rx.recv().unwrap(); println!(got: {},re);
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门