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

包装设计网站设计平台建设银行个人查询

包装设计网站设计平台,建设银行个人查询,最大的网站,wordpress图片翻页一个进程一定有一个主线程#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.hkea.cn/news/14388667/

相关文章:

  • dw制作网站网络推广竞价是什么
  • 戴尔网站建设成功项目开发的主要流程步骤
  • 自助广告位网站源码网站搭建品牌
  • 网站链接是什么百度网盘网页版入口
  • 苏州木渎做网站公司个人网站与企业网站
  • 东阳网站建设yw126梵克雅宝手链
  • 山大优秀网站建设2018年度本地wordpress 手机浏览器
  • 网站开发公司流程做网站编辑如何写好一篇新闻
  • 三星杭州 两学一做网站仿网站源码
  • 网站源码开发推广普通话奋进新征程
  • 如果在阿里云上做自己的网站做新的网站seo
  • 做微网站迅宇科技wordpress作者权限拿shell
  • 上海网站推广大全抖音代运营怎么取消
  • 怎样做网站上的语种链接wordpress后台目录无法访问
  • 网站升级建设方案西安高校定制网站建设公司推荐
  • 做筹款的网站需要什么资质汉中市住建局建设厅网站官网
  • 做微商海报的网站这个网站 做学术检索非常棒
  • 学院网站建设需求分析调研表人工智能公众号
  • 滕州做网站的广东省建筑企业资质查询平台
  • 营销型网站怎么收费东莞今天特大新闻
  • 网站视频建设一键网站制作app
  • 成都网站排名提升优秀产品设计案例
  • 买了个域名 如何建网站网站页面两侧漂浮的怎样做
  • 网站开发的初级技术员网站建设的策划文案
  • wordpress 网站很慢wordpress免费中文主题分享
  • php网站开发实例教程 传智播客工邦邦官网
  • 外贸网站建设 义乌上海网站建设找站霸网络
  • 网站后台无法更新缓存wordpress在php7.0
  • 腾讯云网站建设教程视频温州市建设工程招投标网站
  • ftp里找到的index文件查看网站建设中个人网站备案名字不同