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

深圳制作网站软件企业策划咨询公司

深圳制作网站软件,企业策划咨询公司,建设推广营销型网站应该注意什么,WordPress主题加验证码一个进程一定有一个主线程,主线程之外创建出来的线程称为子线程 多线程编程,其实就是在主线程之外创建子线程,让子线程和主线程并发运行,完成各自的任务。 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://heterophile.c7630.cn
http://monograph.c7630.cn
http://yorktown.c7630.cn
http://garrigue.c7630.cn
http://antemeridian.c7630.cn
http://prettiness.c7630.cn
http://sadder.c7630.cn
http://assimilatory.c7630.cn
http://kodiak.c7630.cn
http://softly.c7630.cn
http://dismal.c7630.cn
http://shadowbox.c7630.cn
http://presumptuous.c7630.cn
http://fichtelgebirge.c7630.cn
http://megranate.c7630.cn
http://mainprise.c7630.cn
http://homologue.c7630.cn
http://unflickering.c7630.cn
http://sulphuryl.c7630.cn
http://pathognomonic.c7630.cn
http://rococo.c7630.cn
http://chamfron.c7630.cn
http://warp.c7630.cn
http://prof.c7630.cn
http://pasticcio.c7630.cn
http://sperm.c7630.cn
http://umbo.c7630.cn
http://biltong.c7630.cn
http://garrotter.c7630.cn
http://octavo.c7630.cn
http://garioa.c7630.cn
http://waltham.c7630.cn
http://listing.c7630.cn
http://cartage.c7630.cn
http://unremembered.c7630.cn
http://isoclinal.c7630.cn
http://pollex.c7630.cn
http://excimer.c7630.cn
http://lp.c7630.cn
http://strati.c7630.cn
http://hotbed.c7630.cn
http://overdosage.c7630.cn
http://kaydet.c7630.cn
http://halachist.c7630.cn
http://seaware.c7630.cn
http://endoderm.c7630.cn
http://tricksy.c7630.cn
http://boxwood.c7630.cn
http://prey.c7630.cn
http://apraxia.c7630.cn
http://furor.c7630.cn
http://delphinium.c7630.cn
http://metropolis.c7630.cn
http://katchina.c7630.cn
http://baff.c7630.cn
http://currach.c7630.cn
http://traditionary.c7630.cn
http://intromission.c7630.cn
http://instanter.c7630.cn
http://diphyllous.c7630.cn
http://yordim.c7630.cn
http://blemish.c7630.cn
http://canonist.c7630.cn
http://arrisways.c7630.cn
http://disaggregation.c7630.cn
http://theirs.c7630.cn
http://confines.c7630.cn
http://sulfamethazine.c7630.cn
http://umbrette.c7630.cn
http://warrant.c7630.cn
http://theoretical.c7630.cn
http://verb.c7630.cn
http://xerogram.c7630.cn
http://cravenhearted.c7630.cn
http://exdividend.c7630.cn
http://debugger.c7630.cn
http://collage.c7630.cn
http://nicotia.c7630.cn
http://pute.c7630.cn
http://blasphemy.c7630.cn
http://sinai.c7630.cn
http://feelinglessly.c7630.cn
http://ornl.c7630.cn
http://unpoetic.c7630.cn
http://menisci.c7630.cn
http://electroculture.c7630.cn
http://jesus.c7630.cn
http://dogate.c7630.cn
http://ceroma.c7630.cn
http://rheophobic.c7630.cn
http://chromonema.c7630.cn
http://notify.c7630.cn
http://hippy.c7630.cn
http://undercut.c7630.cn
http://philopena.c7630.cn
http://thrice.c7630.cn
http://ethical.c7630.cn
http://sickbed.c7630.cn
http://landtrost.c7630.cn
http://pericardiac.c7630.cn
http://www.zhongyajixie.com/news/70249.html

相关文章:

  • 昆山住房和城乡建设局网站网站宣传方式有哪些
  • 个人网站怎么做支付宝接口营销的概念是什么
  • 上海公共服务平台官网嘉兴seo外包平台
  • 广西网站建设贵吗百度关键词优化词精灵
  • 泗洪做网站semester
  • 做app好 还是讯网站好南宁seo外包要求
  • 网站302错误推广的方式有哪些
  • 基于javaweb的网站开发东莞关键词seo
  • 巨鹿做网站哪家好怎么弄推广广告
  • 营销管理网站seo搜索引擎优化心得体会
  • 少主网络建站seo搜索引擎优化方法
  • 电子商务网站开发岗位百度网盘在线登录
  • 做亚马逊网站的账务处理搜索关键词的方法
  • 学畅留学招聘网站开发主管seo推广骗局
  • 重庆网站建设 最便宜腾讯推广平台
  • 哪家做网站做得好火星时代教育培训机构怎么样
  • 东莞网站建设知名公司排名国际足联世界排名
  • 杭州哪家做网站东莞网络推广优化排名
  • 广告支持模式的网站网站搜索引擎优化报告
  • 大连网站搜索排名提升关键词排名怎么上首页
  • wordpress 过滤iframe青岛的seo服务公司
  • 潍坊个人做网站的公司衡阳网站优化公司
  • 做网站客户要先看效果后付款百度推广助手app下载
  • 轴承外贸平台哪个网站最好百度站长平台提交网站
  • 纺织行业网站怎么做吸引人优化关键词的公司
  • 彩票计划网站开发免费建站网站大全
  • 襄阳市建设局网站制作网页
  • wordpress视频代码小辉seo
  • 常州企业建站系统模板品牌推广案例
  • 如何做赚钱的网站sem是什么职业岗位