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

徐州cms建站系统中卫网架钢结构设计

徐州cms建站系统,中卫网架钢结构设计,苏州网页制作报价,微网站生成app文章目录 Rust对多线程的支持std::thread::spawn创建线程线程与 move 闭包 使用消息传递在线程间传送数据std::sync::mpsc::channel()for received in rx接收两个producer 共享状态并发std::sync::Mutex在多个线程间共享Mutex,使用std::sync::Arc 参考 Rust对多线程…

文章目录

  • Rust对多线程的支持
      • std::thread::spawn创建线程
      • 线程与 move 闭包
    • 使用消息传递在线程间传送数据
      • std::sync::mpsc::channel()
      • for received in rx接收
      • 两个producer
  • 共享状态并发
    • std::sync::Mutex
    • 在多个线程间共享Mutex,使用std::sync::Arc
  • 参考

Rust对多线程的支持

rust默认仅支持一对一线程,也就是说操作系统线程。

可以引入crate包来使用绿色线程。

std::thread::spawn创建线程

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();
}

编译

 cargo run
warning: unused `Result` that must be used--> src/main.rs:17:5|
17 |     handle.join();|     ^^^^^^^^^^^^^|= note: this `Result` may be an `Err` variant, which should be handled= note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value|
17 |     let _ = handle.join();|     +++++++warning: `smartPtr` (bin "smartPtr") generated 1 warningFinished `dev` profile [unoptimized + debuginfo] target(s) in 0.00sRunning `target/debug/smartPtr`
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 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!

线程与 move 闭包

use std::thread;fn main() {let v = vec![1, 2, 3];let handle = thread::spawn(move || {println!("Here's a vector: {:?}", v);});handle.join().unwrap();
}

编译

 cargo runBlocking waiting for file lock on build directoryCompiling smartPtr v0.1.0 (/home/wangji/installer/rust/bobo/smartPtr)Finished `dev` profile [unoptimized + debuginfo] target(s) in 19.07sRunning `target/debug/smartPtr`
Here's a vector: [1, 2, 3]

使用消息传递在线程间传送数据

std::sync::mpsc::channel()

只支持多个生产者和一个消费者

use std::sync::mpsc; //mpsc:multi-producer, single-consumer
use std::thread;fn main() {let (tx, rx) = mpsc::channel();thread::spawn(move || {let val = String::from("hi");tx.send(val).unwrap();});// recv()会阻塞// try_recv()是非阻塞,适合使用loop来尝试接收let received = rx.recv().unwrap();println!("Got: {}", received);
}

for received in rx接收

use std::sync::mpsc;
use std::thread;
use std::time::Duration;fn main() {let (tx, rx) = mpsc::channel();thread::spawn(move || {let vals = vec![String::from("hi"),String::from("from"),String::from("the"),String::from("thread"),];for val in vals {tx.send(val).unwrap();thread::sleep(Duration::from_secs(1));}});// 当发送方tx结束了,接收方rx就会结束(for循环也会结束)for received in rx {println!("Got: {}", received);}
}

两个producer

use std::sync::mpsc;
use std::thread;
use std::time::Duration;fn main() {// --snip--let (tx, rx) = mpsc::channel();let tx1 = tx.clone(); //tx和tx1都连接到mpsc::channel()thread::spawn(move || {let vals = vec![String::from("hi"),String::from("from"),String::from("the"),String::from("thread"),];for val in vals {tx1.send(val).unwrap();thread::sleep(Duration::from_secs(1));}});thread::spawn(move || {let vals = vec![String::from("more"),String::from("messages"),String::from("for"),String::from("you"),];for val in vals {tx.send(val).unwrap();thread::sleep(Duration::from_secs(1));}});for received in rx {println!("Got: {}", received);}// --snip--
}

共享状态并发

std::sync::Mutex

use std::sync::Mutex;fn main() {let m = Mutex::new(5);{// m.lock()会阻塞当前线程,获取锁位置let mut num = m.lock().unwrap();*num = 6;// 退出时会自动释放锁}println!("m = {:?}", m);
}

编译及运行

 cargo runCompiling smartPtr v0.1.0 (/home/wangji/installer/rust/bobo/smartPtr)Finished `dev` profile [unoptimized + debuginfo] target(s) in 6.57sRunning `target/debug/smartPtr`
m = Mutex { data: 6, poisoned: false, .. }

在多个线程间共享Mutex,使用std::sync::Arc

use std::sync::Arc;
use std::sync::Mutex;
use std::thread;fn main() {// Mutex于Arc经常一起使用// Rc::new可以让Mutex拥有多个所有者let counter = Arc::new(Mutex::new(0));let mut handles = vec![];for _ in 0..10 {let counter = Arc::clone(&counter); //Rc::clone不是真正的clone,只是增加引用计数let handle = thread::spawn(move || {let mut num = counter.lock().unwrap(); //counter.lock()可以获得可变的引用(类似于RefCell智能指针)*num += 1;});handles.push(handle);}for handle in handles {handle.join().unwrap();}println!("Result: {}", *counter.lock().unwrap());
}

编译

 cargo runCompiling smartPtr v0.1.0 (/home/wangji/installer/rust/bobo/smartPtr)Finished `dev` profile [unoptimized + debuginfo] target(s) in 5.85sRunning `target/debug/smartPtr`
Result: 10

参考

  • 第16章~创建线程
http://www.yayakq.cn/news/151262/

相关文章:

  • 北京如何优化网站网站建设模板
  • 免费注册网站域名可以用吗网站 设置特殊的字体
  • 公司里面有人员增减要去哪个网站做登记吴江注册公司
  • 给网站添加关键词毕业设计做视频网站设计
  • 装饰公司营销网站模板jQuery EasyUI网站开发实战
  • php电子商务网站建设宿迁做网站优化
  • 商城网站建站方案建设工程标准合同范本
  • 做好一个网站需要多久企业网站设计与建设
  • python 网站架构黄冈网站设计推广哪家好
  • 龙岗商城网站建设哪家便宜加强学院网站的建设与管理
  • 响应式网站建设哪家好成都网站建设创意
  • 知名手机网站优化内容
  • 长治网站制作怎么做成都广告设计公司排名
  • 网站seo优化推广教程企业营销型网站推广
  • 做班级网站代码怎么看网站有没有做百度推广
  • 信用湘潭网站国内十大咨询公司排名
  • 泡沫制品技术支持东莞网站建设爱站网爱情电影网
  • 手机网站js电话悬浮网站设计过程介绍
  • 用dw建设个人网站视频佛山网站设计联系方式
  • 兴科cms网站建设系统vr哪家公司做得好
  • 佛山南海网站建设设计logo网站免
  • 大专学网站开发服务器禁止ip访问网站
  • 湛江的网站wordpress 判断语言
  • 山西seo网站设计微信优惠券网站怎么做的
  • 建设法规网站wordpress外贸网站模板
  • 校网站建设方案网址大全123设为主页
  • 泰州企业网站模板建站济南济阳网站建设
  • 寻找做网站的合作伙伴北京惠州网站设计方案
  • 广东集团网站建设互联网营销是做什么的
  • ps做网站的视频王也气质头像