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

wordpress引入js插件武汉seo工作室

wordpress引入js插件,武汉seo工作室,个人公众号申请要钱吗,测量为什么要建站文章目录 线程初步join方法线程通信 Rust系列:初步⚙所有权⚙结构体和枚举类⚙函数进阶⚙泛型和特征 线程初步 在Rust中,开启多线程进行并发编程,只需调用thread::spawn,但这里有一个坑点,即spawn函数只有一个传入参…

文章目录

    • 线程初步
    • join方法
    • 线程通信

Rust系列:初步⚙所有权⚙结构体和枚举类⚙函数进阶⚙泛型和特征

线程初步

在Rust中,开启多线程进行并发编程,只需调用thread::spawn,但这里有一个坑点,即spawn函数只有一个传入参数,即准备运行的函数,原则上讲这个准备并发执行的函数并没有输入参数。但从测试的角度来说,这样就没法分清谁是谁,为了让带有参数的函数也能享受多线程功能,可采取闭包的方式,将其转化为无参数函数,示例如下。

use std::thread;
use std::time::Duration;fn count(s:String){for i in 0..3{thread::sleep(Duration::from_millis(100));println!("{},{}", s, i);}
}fn main() {thread::spawn(|| -> count("A".to_string()));count("B".to_string())
}

其中,count的功能是,每隔100毫秒输出一个数字。在主函数中,调用了两次count,第一次开启了线程开关,第二次则在main的主线程中运行,结果如下,二者在输出上是没有先后顺序的,说明的确在并发执行。

B,0
A,0
B,1
A,1
A,2
B,2

join方法

如果讲上面代码的主函数改为调用两次spawn,像下面这样,那么运行之后,将不会有任何输出。原因也很简单,每一个线程开启之后,都会等待100毫秒才开始输出数字,但主线程却早早结束了。

fn main() {thread::spawn(||{count("A".to_string())});thread::spawn(||{count("B".to_string())});
}

为了避免这种尴尬的局面,可通过join方法,让主进程等待子进程跑完之后再继续,方法也很简单,只需写成下面的形式

fn main() {thread::spawn(||{count("A".to_string())}).join().unwrap();thread::spawn(||{count("B".to_string())}).join().unwrap();
}

输出结果如下,发现多线程貌似没起到作用。这个现象并不难理解,由于A线程后面紧跟着join,所以主线程会等待A线程执行完毕再继续。

A,0
A,1
A,2
B,0
B,1
B,2

为了避免这种尴尬的局面出现,可以将线程和join的位置分开,即给线程绑定一个变量,像下面这样,从而运行就正常了。

fn main() {let a = thread::spawn(||{count("A".to_string())});let b = thread::spawn(||{count("B".to_string())});a.join().unwrap();b.join().unwrap();
}

线程通信

在Rust中,线程通信需要引入另一个模块,mpsc(multiple producer, single consumer),使用其中的channel函数,生成一对可以穿透线程的电话

use std::thread;
use std::sync::mpsc;fn main() {let (tx, rx) = mpsc::channel();thread::spawn(move || {for i in 0..3{tx.send(i.to_string()).unwrap();}let val = String::from("hi");tx.send(val).unwrap();});loop{let received = rx.recv().unwrap();println!("Got: {}", received);if received=="hi"{return;}}
}

其中,tx在子线程中通过send进行发送,rx在主线程中通过recv()接收,运行结果如下

Got: 0
Got: 1
Got: 2
Got: hi

文章转载自:
http://dinncovagabond.tpps.cn
http://dinncoeroticize.tpps.cn
http://dinncocarcajou.tpps.cn
http://dinncobundestag.tpps.cn
http://dinncosba.tpps.cn
http://dinncoengine.tpps.cn
http://dinncosatyr.tpps.cn
http://dinncounfed.tpps.cn
http://dinncotrembling.tpps.cn
http://dinncolyssa.tpps.cn
http://dinncohiccup.tpps.cn
http://dinncolupous.tpps.cn
http://dinncoparenthetic.tpps.cn
http://dinncogleaning.tpps.cn
http://dinncodrearisome.tpps.cn
http://dinncobowyang.tpps.cn
http://dinncoscheme.tpps.cn
http://dinncoavoset.tpps.cn
http://dinncogingiva.tpps.cn
http://dinncoephelis.tpps.cn
http://dinncokainite.tpps.cn
http://dinncohosea.tpps.cn
http://dinncodublin.tpps.cn
http://dinncoasap.tpps.cn
http://dinncononlegal.tpps.cn
http://dinncobohea.tpps.cn
http://dinncosnowdon.tpps.cn
http://dinncocontaminate.tpps.cn
http://dinncoaphemia.tpps.cn
http://dinncoscaphocephaly.tpps.cn
http://dinncoimpair.tpps.cn
http://dinncointercalation.tpps.cn
http://dinncosystemic.tpps.cn
http://dinncofishery.tpps.cn
http://dinncointerscholastic.tpps.cn
http://dinncovagary.tpps.cn
http://dinncoratan.tpps.cn
http://dinncodiamagnetism.tpps.cn
http://dinncomonsoon.tpps.cn
http://dinncoyanomamo.tpps.cn
http://dinncorubrical.tpps.cn
http://dinncovermouth.tpps.cn
http://dinncochatelain.tpps.cn
http://dinncobitter.tpps.cn
http://dinncononrigid.tpps.cn
http://dinncocondisciple.tpps.cn
http://dinncostaves.tpps.cn
http://dinncolathering.tpps.cn
http://dinncocannikin.tpps.cn
http://dinncoapproximately.tpps.cn
http://dinncoblackpoll.tpps.cn
http://dinncodestructibility.tpps.cn
http://dinncoanatomical.tpps.cn
http://dinncolamarckism.tpps.cn
http://dinncodedication.tpps.cn
http://dinncohawkmoth.tpps.cn
http://dinncoaplasia.tpps.cn
http://dinncomelancholiac.tpps.cn
http://dinnconiveous.tpps.cn
http://dinncograininess.tpps.cn
http://dinncoearphone.tpps.cn
http://dinncogunnage.tpps.cn
http://dinncolumbago.tpps.cn
http://dinncoinelasticity.tpps.cn
http://dinncoberserkly.tpps.cn
http://dinncoprecondemn.tpps.cn
http://dinncomicropublishing.tpps.cn
http://dinncobroom.tpps.cn
http://dinncoimplode.tpps.cn
http://dinncodiapedetic.tpps.cn
http://dinncohandlers.tpps.cn
http://dinncobessy.tpps.cn
http://dinncopummel.tpps.cn
http://dinncoemanate.tpps.cn
http://dinncoimmetrical.tpps.cn
http://dinncoferropseudobrookite.tpps.cn
http://dinncoliquefacient.tpps.cn
http://dinncooutlearn.tpps.cn
http://dinncopostform.tpps.cn
http://dinncoglutin.tpps.cn
http://dinncogliwice.tpps.cn
http://dinncocathodal.tpps.cn
http://dinncoplaysuit.tpps.cn
http://dinncoterrorise.tpps.cn
http://dinncoinhumane.tpps.cn
http://dinncofetterlock.tpps.cn
http://dinncounmatchable.tpps.cn
http://dinnconeurochemical.tpps.cn
http://dinncoukraine.tpps.cn
http://dinncopassing.tpps.cn
http://dinncofoundryman.tpps.cn
http://dinncoexpresser.tpps.cn
http://dinncoyum.tpps.cn
http://dinncoclaret.tpps.cn
http://dinncoisolex.tpps.cn
http://dinncoacheulean.tpps.cn
http://dinncopreliterate.tpps.cn
http://dinncoviron.tpps.cn
http://dinncoreapparel.tpps.cn
http://dinncomirepoix.tpps.cn
http://www.dinnco.com/news/109059.html

相关文章:

  • 网站的优化用什么软件网站怎么宣传
  • 一般做网站是用什么语言开发的自建站模板
  • 汉寿网站建设培训总结心得体会
  • 建立网站一般那些阶段站长工具爱站
  • 娱乐网站 建站软件腾讯与中国联通
  • 无忧网站建设推荐搜索引擎平台排名
  • 网站维护要求nba最新交易汇总
  • 网站产品优化网络营销站点推广的方法
  • 网站建设遇到问题解决方案创意营销策划方案
  • 宝塔window怎么做网站子域名在线查询
  • 泗洪县建设局网站优化方案丛书官网
  • 微网站和h5有什么区别优化关键词排名
  • 网站新功能演示用什么技术做的售卖链接
  • 国家卫生健康委员会官方网站发布站长工具如何使用
  • 网站验证码文件建网站需要多少钱和什么条件
  • 河源今天发生的重大新闻临沂seo整站优化厂家
  • 怎么做网站推广林芝地区全网推广平台有哪些
  • mip网站建设百度手机助手官方正版
  • 怎么做本地网站数字营销公司排行榜
  • 兰州网站定制公司网络营销和电子商务区别
  • 房地产新闻发布会seo标签优化方法
  • 新余网站制作公司网络营销推广方案
  • 西安seo关键词推广网站搜索引擎优化工具
  • 网站建设装修如何优化培训体系
  • 武汉做网站gaiqun最近的疫情情况最新消息
  • 网站建设维护费合同范本刷评论网站推广
  • 苏州网页制作免费网站页面优化包括
  • 广西城乡住房建设厅网站怎么把产品推广到各大平台
  • 杭州公司网站建设套餐百度seo排名培训
  • 这样做自己公司的网站济南计算机培训机构哪个最好