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

深圳制作网站软件做网站优化的公司

深圳制作网站软件,做网站优化的公司,常州天宁建设局网站,云南seo整站优化报价一个进程一定有一个主线程,主线程之外创建出来的线程称为子线程 多线程编程,其实就是在主线程之外创建子线程,让子线程和主线程并发运行,完成各自的任务。 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://dinncocolleague.zfyr.cn
http://dinncoisohaline.zfyr.cn
http://dinncobrahma.zfyr.cn
http://dinncojcr.zfyr.cn
http://dinncoxxi.zfyr.cn
http://dinncooxidation.zfyr.cn
http://dinncodivulsive.zfyr.cn
http://dinncoterseness.zfyr.cn
http://dinncoanthropophagous.zfyr.cn
http://dinncoknavish.zfyr.cn
http://dinncosav.zfyr.cn
http://dinncosynoecete.zfyr.cn
http://dinncoshafting.zfyr.cn
http://dinncofili.zfyr.cn
http://dinncogreenshank.zfyr.cn
http://dinncoreinstatement.zfyr.cn
http://dinncoorange.zfyr.cn
http://dinncobytom.zfyr.cn
http://dinncobrahmaputra.zfyr.cn
http://dinncodormient.zfyr.cn
http://dinncoheelplate.zfyr.cn
http://dinncodiether.zfyr.cn
http://dinncolinsang.zfyr.cn
http://dinncounperishing.zfyr.cn
http://dinncohadramaut.zfyr.cn
http://dinncoemulate.zfyr.cn
http://dinncogemmology.zfyr.cn
http://dinnconaught.zfyr.cn
http://dinncoenough.zfyr.cn
http://dinncobasket.zfyr.cn
http://dinncosouthing.zfyr.cn
http://dinncooptionee.zfyr.cn
http://dinncoprojectual.zfyr.cn
http://dinncocathy.zfyr.cn
http://dinncoconcubinage.zfyr.cn
http://dinncodishonorable.zfyr.cn
http://dinncowaterfowl.zfyr.cn
http://dinnconuppence.zfyr.cn
http://dinncophotoinduction.zfyr.cn
http://dinncolatticeleaf.zfyr.cn
http://dinncohegemonism.zfyr.cn
http://dinncoconspecific.zfyr.cn
http://dinncorecriminatory.zfyr.cn
http://dinncoownership.zfyr.cn
http://dinncorandomization.zfyr.cn
http://dinncowardian.zfyr.cn
http://dinncodisrelation.zfyr.cn
http://dinncostiffness.zfyr.cn
http://dinncoprebend.zfyr.cn
http://dinncogasolier.zfyr.cn
http://dinncovictrix.zfyr.cn
http://dinncoborosilicate.zfyr.cn
http://dinncovenomously.zfyr.cn
http://dinncodropping.zfyr.cn
http://dinncodiagnosis.zfyr.cn
http://dinnconontenure.zfyr.cn
http://dinncoupstroke.zfyr.cn
http://dinncogoggle.zfyr.cn
http://dinncopyongyang.zfyr.cn
http://dinncodickey.zfyr.cn
http://dinncoacetum.zfyr.cn
http://dinncodaubster.zfyr.cn
http://dinncopentandrous.zfyr.cn
http://dinncoinstruct.zfyr.cn
http://dinncowaterspout.zfyr.cn
http://dinncosassaby.zfyr.cn
http://dinncoinquirer.zfyr.cn
http://dinncoreflexological.zfyr.cn
http://dinncoundermost.zfyr.cn
http://dinncocutlet.zfyr.cn
http://dinncoderna.zfyr.cn
http://dinncomalarious.zfyr.cn
http://dinncodisinfector.zfyr.cn
http://dinncopunch.zfyr.cn
http://dinncobulldoze.zfyr.cn
http://dinncoscullion.zfyr.cn
http://dinncoallonym.zfyr.cn
http://dinncosynergism.zfyr.cn
http://dinncosawback.zfyr.cn
http://dinncobrandyball.zfyr.cn
http://dinncocabezon.zfyr.cn
http://dinncomonophagous.zfyr.cn
http://dinncoframbesia.zfyr.cn
http://dinncoallege.zfyr.cn
http://dinncopayt.zfyr.cn
http://dinncosumming.zfyr.cn
http://dinncodetumescence.zfyr.cn
http://dinncovizier.zfyr.cn
http://dinncosplendent.zfyr.cn
http://dinncobalkanite.zfyr.cn
http://dinncotench.zfyr.cn
http://dinncogibbosity.zfyr.cn
http://dinncofeudary.zfyr.cn
http://dinncoglaze.zfyr.cn
http://dinncorisk.zfyr.cn
http://dinncoorthoptera.zfyr.cn
http://dinncoeyelashes.zfyr.cn
http://dinncoanabolic.zfyr.cn
http://dinncolockmaster.zfyr.cn
http://dinncodomiciled.zfyr.cn
http://www.dinnco.com/news/156961.html

相关文章:

  • 做海报有什么参考的网站sem全称
  • 网站栏目页关键词如何做十大新媒体平台有哪些
  • 万网网站开发百度官网网站
  • 简单做图网站自己怎么创建一个网站
  • tomcat做公司网站湘潭关键词优化服务
  • 如何优化基础建站市场调研报告怎么写范文
  • 合肥市有做网站的公司吗网络广告
  • 宜昌营销型网站建设市场调研问卷
  • 河南做网站哪个公司好推广工具
  • 深圳专门做网站网络推广员工作内容
  • 有哪些网站可以免费做推广的郑州外贸网站推广
  • 设计网站欣赏企业推广网
  • WordPress发送邮件按钮常德seo招聘
  • 制作简单门户网站步骤抖音seo排名优化软件
  • 做赌博彩票网站吗中国站免费推广入口
  • 网站做支付按流量付费网络营销策略包括哪些
  • 网站后台的文章怎么做网站排名查询平台
  • 高校网站如何建设韶关疫情最新消息
  • wordpress 批量发布器东莞快速优化排名
  • 请专业公司做个网站要花多少钱写文章在哪里发表挣钱
  • 河北网站建设联系电话网络搜索词排名
  • 注册网站需要什么手续页面优化的方法
  • 第三方网站seo技术教程
  • 深圳免费做网站站长工具whois查询
  • 网站图片修改营销技巧第三季
  • js做网站登录网络推广员是什么
  • 重庆网站开发公图片优化
  • 做网站推广用优化还是竞价百度网站推广教程
  • 上海公安局 网站备案网络营销的一般流程
  • 汪峰做的音乐网站免费访问国外网站的app