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

制作网站软件不懂代码可以制作吗学习搭建网站

制作网站软件不懂代码可以制作吗,学习搭建网站,网站建设飠金手指下拉,龙岗网站建设-信科网络文章目录 Rust对多线程的支持std::thread::spawn创建线程线程与 move 闭包 使用消息传递在线程间传送数据std::sync::mpsc::channel()for received in rx接收两个producer 共享状态并发std::sync::Mutex在多个线程间共享Mutex#xff0c;使用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!(Heres 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 Heres a vector: [1, 2, 3]使用消息传递在线程间传送数据 std::sync::mpsc::channel() 只支持多个生产者和一个消费者 use std::sync::mpsc; //mpscmulti-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.yingshimen.cn/news/70805/

相关文章:

  • 青海 网站开发 app做英语网站
  • 北京网站建设要多少钱湖北企业网站建设公司
  • 建设人力资源服务网站工作方案网站备案需要多少天
  • 做网站用突发性实例可以吗wordpress的登录地址修改
  • 免费做网站用什么软件营销策划公司品牌
  • 电子商务网站建设评价论文软件开发网站建设科技有限公司
  • 做国际网站有哪些筹备网站建设
  • 网站建设公司账户搭建如何制作一个网页页面
  • 手机app与网站链接民族服装的网站建设
  • 衡水网站设计费用企业组网设计
  • 网站站长统计怎么弄天猫与京东的网站建设管理
  • 兼容模式网站错位设计logo公司
  • 深圳做网站哪家公司好汽车之家官网网页版入口
  • 手机网站内容规划做企业网站选百度云还是阿里云
  • 我想卖自己做的鞋子 上哪个网站好中企动力做的网站怎么样
  • 网站建设那个公司好数据分析
  • 可以做配音兼职的网站wordpress页脚改颜色
  • 建设银行反钓鱼网站关键词全网搜索指数
  • pinterest网站怎么进上海网站定制费用
  • 重庆响应式网站多少钱自己做网站麻烦吗
  • 做个爬架网站如何做东莞网站公司星鑫
  • 建设网站 xp利用小米路由器mini做网站
  • php程序员做企业网站wordpress知更鸟主题怎么用
  • 在外汇管理网站做网站开发菜鸟适合用什么软件
  • 天津市北辰区建设与管理局网站怎么用小旋风网站建设教程
  • 直播软件下载网站个体工商户注册网站
  • 北京招聘网站开发网站建设忄金手指快速
  • 电子商城网站如何建设自己做的网站显示不安全怎么回事
  • 高校网站建设规范数据营销系统
  • html5网站有点电商具体是做什么的上班