鲜花网站建设报告,网络外包,网站开发常用标签,静态网站如何做优化一个进程一定有一个主线程#xff0c;主线程之外创建出来的线程称为子线程 多线程编程#xff0c;其实就是在主线程之外创建子线程#xff0c;让子线程和主线程并发运行#xff0c;完成各自的任务。 Rust语言支持多线程编程。
Rust语言标准库中的 std::thread 模块用于多线…一个进程一定有一个主线程主线程之外创建出来的线程称为子线程 多线程编程其实就是在主线程之外创建子线程让子线程和主线程并发运行完成各自的任务。 Rust语言支持多线程编程。
Rust语言标准库中的 std::thread 模块用于多线程编程。 std::thread 提供很很多方法用于创建线程、管理线程和结束线程。
一、创建线程
使用std::thread::spawn()方法创建一个线程。
pub fn spawnF, T(f: F) - JoinHandleT参数 f 是一个闭包是线程要执行的代码。
范例
use std::thread; // 导入线程模块
use std::time::Duration; // 导入时间模块
fn main() {//创建一个新线程thread::spawn(|| {for i in 1..10 {println!(hi number {} from the spawned thread!, i);thread::sleep(Duration::from_millis(1));}});// 主线程要执行的代码for i in 1..5 {println!(hi number {} from the main thread!, i);thread::sleep(Duration::from_millis(1));}
}
编译运行结果如下
hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the main thread!
hi number 2 from the spawned thread!
hi number 3 from the main thread!
hi number 3 from the spawned thread!
hi number 4 from the spawned thread!
hi number 4 from the main thread!咦执行结果好像出错了 是吗 当主线程执行结束那么就会自动关闭创建出来的子线程。 上面的代码我们调用 thread::sleep() 函数强制线程休眠一段时间这就允许不同的线程交替执行。 虽然某个线程休眠时会自动让出cpu但并不保证其它线程会执行。这取决于操作系统如何调度线程。 这个范例的输出结果是随机的主线程一旦执行完成程序就会自动退出不会继续等待子线程。这就是子线程的输出结果不全的原因。
二、让主线程等待子线程
默认情况下主线程并不会等待子线程执行完毕。为了避免这种情况我们可以让主线程等待子线程执行完毕然后再继续执行。
Rust标准库提供了 join() 方法用于把子线程加入主线程等待队列。
spawnF, T(f: F) - JoinHandleT范例
use std::thread;
use std::time::Duration;
fn main() {let handle thread::spawn(|| {for i in 1..10 {println!(hi number {} from the spawned thread!, i);thread::sleep(Duration::from_millis(1));}});for i in 1..5 {println!(hi number {} from the main thread!, i);thread::sleep(Duration::from_millis(1));}handle.join().unwrap();
}
编译运行结果如下
hi number 1 from the main thread!
hi number 1 from the spawned thread!
hi number 2 from the spawned thread!
hi number 2 from the main thread!
hi number 3 from the spawned thread!
hi number 3 from the main thread!
hi number 4 from the main thread!
hi number 4 from the spawned thread!
hi number 5 from the spawned thread!
hi number 6 from the spawned thread!
hi number 7 from the spawned thread!
hi number 8 from the spawned thread!
hi number 9 from the spawned thread!从输出结果来看主线程和子线程交替执行。 主线程等待子线程执行完毕是因为调用了 join() 方法。
三、move强制所有权迁移
这是一个经常遇到的情况 实例
use std::thread;
fn main() {let s hello;let handle thread::spawn(|| {println!({}, s);});handle.join().unwrap();
}在子线程中尝试使用当前函数的资源这一定是错误的因为所有权机制禁止这种危险情况的产生它将破坏所有权机制销毁资源的一定性。我们可以使用闭包的move关键字来处理 实例
use std::thread;
fn main() {let s hello;let handle thread::spawn(move || {println!({}, s);});handle.join().unwrap();
}四、消息传递
使用通道传递消息通道有两部分组成一个发送者transmitter和一个接收者receiver。 std::sync::mpsc包含了消息传递的方法 实例
use std::thread;
use std::sync::mpsc;
fn main() {let (tx, rx) mpsc::channel();thread::spawn(move || {let val String::from(hi);tx.send(val).unwrap();});let received rx.recv().unwrap();println!(Got: {}, received);
}
运行结果
Got: hi子线程获得了主线程的发送者tx并调用了它的send方法发送了一个字符串然后主线程就通过对应的接收者rx接收到了。 文章转载自: http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.hphfy.cn.gov.cn.hphfy.cn http://www.morning.zkqsc.cn.gov.cn.zkqsc.cn http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.pwwdp.cn.gov.cn.pwwdp.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.mxhys.cn.gov.cn.mxhys.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.fgxnb.cn.gov.cn.fgxnb.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.ydgzj.cn.gov.cn.ydgzj.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.kxltf.cn.gov.cn.kxltf.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.nktgj.cn.gov.cn.nktgj.cn http://www.morning.qkgwx.cn.gov.cn.qkgwx.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.tdcql.cn.gov.cn.tdcql.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn http://www.morning.qsy39.cn.gov.cn.qsy39.cn http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.wpsfc.cn.gov.cn.wpsfc.cn http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.rhmk.cn.gov.cn.rhmk.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.dnqlba.cn.gov.cn.dnqlba.cn http://www.morning.bybhj.cn.gov.cn.bybhj.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.bnfjh.cn.gov.cn.bnfjh.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.tpnxr.cn.gov.cn.tpnxr.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.nbdtdjk.cn.gov.cn.nbdtdjk.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.slpcl.cn.gov.cn.slpcl.cn http://www.morning.yskhj.cn.gov.cn.yskhj.cn http://www.morning.rnmyw.cn.gov.cn.rnmyw.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.hsflq.cn.gov.cn.hsflq.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn