网站建设富库,网页浏览器是windows系统自带的是,网站html代码,深圳商业网站建设案例对于终止和挂起线程#xff0c;您可以使用通道 .外部终止在工作循环的每次迭代中#xff0c;我们检查是否有人通过渠道通知我们 . 如果是#xff0c;或者如果通道的另一端超出范围#xff0c;我们就会打破循环 .use std::io::{self, BufRead};use std::sync::mpsc::{self, …对于终止和挂起线程您可以使用通道 .外部终止在工作循环的每次迭代中我们检查是否有人通过渠道通知我们 . 如果是或者如果通道的另一端超出范围我们就会打破循环 .use std::io::{self, BufRead};use std::sync::mpsc::{self, TryRecvError};use std::thread;use std::time::Duration;fn main() {println!(Press enter to terminate the child thread);let (tx, rx) mpsc::channel();thread::spawn(move || loop {println!(Working...);thread::sleep(Duration::from_millis(500));match rx.try_recv() {Ok(_) | Err(TryRecvError::Disconnected) {println!(Terminating.);break;}Err(TryRecvError::Empty) {}}});let mut line String::new();let stdin io::stdin();let _ stdin.lock().read_line(mut line);let _ tx.send(());}暂停和恢复我们使用 recv() 暂停线程直到某些东西到达通道 . 为了恢复该线程您需要通过该 Channels 发送内容;在这种情况下单位值 () . 如果通道的发送端被丢弃 recv() 将返回 Err(()) - 我们使用它来退出循环 .use std::io::{self, BufRead};use std::sync::mpsc;use std::thread;use std::time::Duration;fn main() {println!(Press enter to wake up the child thread);let (tx, rx) mpsc::channel();thread::spawn(move || loop {println!(Suspending...);match rx.recv() {Ok(_) {println!(Working...);thread::sleep(Duration::from_millis(500));}Err(_) {println!(Terminating.);break;}}});let mut line String::new();let stdin io::stdin();for _ in 0..4 {let _ stdin.lock().read_line(mut line);let _ tx.send(());}}其他工具Channels 是执行这些任务的最简单最自然(IMO)的方式但不是最有效的方法 . 您可以在std::sync模块中找到其他并发原语 . 它们属于比通道更低的级别但在特定任务中可以更有效 .