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

Rust8.2 Fearless Concurrency

Rust学习笔记

Rust编程语言入门教程课程笔记

参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community)

Lecture 16: Fearless Concurrency 无畏并发

src/main.rs

use std::thread;
use std::time::Duration;
use std::sync::mpsc;
use std::sync::{Mutex, Arc};fn main() {//Using Threads to Run Code Simultaneously//Creating a New Thread with spawnlet 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));}// output:// 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!//In this run, the main thread printed first, //even though the print statement from the spawned thread appears first in the code. //And even though we told the spawned thread to print until i is 9, //it only got to 5 before the main thread shut down.//Waiting for All Threads to Finish Using join Handleshandle.join().unwrap();let v = vec![1, 2, 3];let handle = thread::spawn(move || {println!("Here's a vector: {:?}", v);});//move closure: v is moved into the closure// drop(v); // oh no! v is moved into the threadhandle.join().unwrap();//Using Message Passing to Transfer Data Between Threadslet (tx, rx) = mpsc::channel();thread::spawn(move || {let val = String::from("hi");// tx.send(val).unwrap();tx.send(val).unwrap();// println!("val is {}", val); // error: value borrowed here after move});let received = rx.recv().unwrap();//recv blocks the main thread’s execution and waits until a value is sent down the channelprintln!("Got: {}", received);//try_recv: without blocking//Sending Multiple Values and Seeing the Receiver Waitinglet (tx, rx) = mpsc::channel();thread::spawn(move || {let vals = vec![String::from("hello"),String::from("from"),String::from("the"),String::from("thread"),];for val in vals {tx.send(val).unwrap();thread::sleep(Duration::from_secs(1));}});for received in rx {println!("Got: {}", received);}//Shared-State Concurrency//Mutexes: Mutual Exclusion//API: lock, unwrap, Mutex<T> (smart pointer)let m = Mutex::new(5);{let mut num = m.lock().unwrap();*num = 6;// println!("m is {}", m); // error: cannot be formatted with the default formatter}println!("m is {:?}", m);//Sharing a Mutex<T> Between Multiple Threadslet counter = Arc::new(Mutex::new(0));//Arc<T> is an atomically reference counted type//A: atomically, R: reference, C: counted//API in Arc is the same as in Rc let mut handles = vec![];for _ in 0..10 {let counter = Arc::clone(&counter);let handle = thread::spawn(move || {let mut num = counter.lock().unwrap();*num += 1;});handles.push(handle);}for handle in handles {handle.join().unwrap();}println!("Result: {}", *counter.lock().unwrap());//Result: 10//Extensible Concurrency with the Sync and Send Traits//Send: ownership can be transferred between threads//Sync: multiple threads can have references to the same value//Implementing Send and Sync Manually Is Unsafe
}
http://www.lryc.cn/news/236113.html

相关文章:

  • 合并两个有序链表(冒泡排序实现)
  • 【iOS】——知乎日报第五周总结
  • gRPC 四模式之 双向流RPC模式
  • 五分钟,Docker安装kafka 3.5,kafka-map图形化管理工具
  • 2023.11.18html中如何使用input/button进行网页跳转
  • java文件压缩加密,使用流的方式
  • 月子会所信息展示服务预约小程序的作用是什么
  • Windows核心编程 静态库与动态库
  • 【Spring Boot】如何自定义序列化以及反序列器
  • 6 Redis的慢查询配置原理
  • JAVA小游戏 “拼图”
  • Spring 配置
  • 全新酷盒9.0源码:多功能工具箱软件的最新iapp解决方案
  • aspose.cells java合并多个excel
  • 【每日一题】三个无重叠子数组的最大和
  • react之基于@reduxjs/toolkit使用react-redux
  • 基于51单片机水位监测控制报警仿真设计( proteus仿真+程序+设计报告+讲解视频)
  • git基本用法和操作
  • 设计模式-组合模式-笔记
  • Android 弹出自定义对话框
  • (论文阅读40-45)图像描述1
  • 4核8G服务器价格选择轻量还是CVM合适?
  • Selenium操作已经打开的Chrome浏览器窗口
  • 创新研报|新业务发展是CEO推动企业增长的必要选择 – Mckinsey研究
  • rv1126-rv1109-openssh
  • MySQL中json类型,你使用过吗
  • MATLAB 状态空间设计 —— LQG/LQR 和极点配置算法
  • 杭州-区块链前瞻性论坛邀请函​
  • ElasticSearch学习篇6_ES实践与Lucene对比及原理分析技术分享小记
  • mysql练习1