当前位置: 首页 > news >正文

网站建设公司的销售好做吗瑞昌市建设局网站

网站建设公司的销售好做吗,瑞昌市建设局网站,行程卡微信小程序入口,企业介绍微网站怎么做的一个进程一定有一个主线程,主线程之外创建出来的线程称为子线程 多线程编程,其实就是在主线程之外创建子线程,让子线程和主线程并发运行,完成各自的任务。 Rust语言支持多线程编程。 Rust语言标准库中的 std::thread 模块用于多线…

一个进程一定有一个主线程,主线程之外创建出来的线程称为子线程
多线程编程,其实就是在主线程之外创建子线程,让子线程和主线程并发运行,完成各自的任务。
Rust语言支持多线程编程。

Rust语言标准库中的 std::thread 模块用于多线程编程。
std::thread 提供很很多方法用于创建线程、管理线程和结束线程。

一、创建线程

使用std::thread::spawn()方法创建一个线程。

pub fn spawn<F, T>(f: F) -> JoinHandle<T>

参数 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() 方法用于把子线程加入主线程等待队列。

spawn<F, T>(f: F) -> JoinHandle<T>

范例

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.yayakq.cn/news/47485/

相关文章:

  • 广州市网站建设制作费用《网站平台建设》课程实训
  • 无锡哪里有建设网站深圳led网站建设
  • 给别人做彩票网站违法吗网络营销课程建议
  • 毕业设计 建设网站免费公司取名器
  • 南宁信息建设网站asp.net 制作网站开发
  • 网站建设申请报告html网页制作参考文献
  • 为什么谷歌网站打不开万网的网站怎么建设
  • 做企业网站收费河源北京网站建设
  • 企业信息查询网官网太原seo计费管理
  • 常州市新北区建设与管理局网站网页游戏排行2013
  • 高新网站开发多少钱雄安建设集团有限公司网站
  • 网站建设心得感想企业网站建设要求标准说明
  • 大数据平台建站网站建设方案书 腾讯
  • 公司支付网站建设费进什么费用点评类网站建设
  • 国外网站为什么不用备案wordpress数据库加速插件
  • 做dj网站能赚钱吗汕头网站建设哪里找
  • php做网站有哪些好处南宁市流量点击推广平台
  • 做淘客网站用什么服务器好seo文章关键词怎么优化
  • 网站建设规划案例网站设计公司如何盈利
  • 天津市工程建设交易网站查汗国加密软件代理
  • 企业网站源码去一品资源网企业网站的规划与建设
  • WordPress设置腾讯企业邮箱网站优化方案基本流程
  • 对网站建设公司说高端网站建设公司哪家更专业
  • 不同域名一样的网站自己的域名可以转给做网站的账号吗
  • 学校教务网站的设计与实现wordpress允许搜索
  • 爱站关键词挖掘软件提供中小企业网站建设
  • 做烘培的网站有哪些产品如何做网站地图
  • 宁波网站制作公司哪家好做网站的网址
  • 原创文学网站建设2022年企业所得税政策
  • 一个网站可以有几个域名茂名市建设银行网站