当前位置:网站首页>Multi thread safe reference arc of rust
Multi thread safe reference arc of rust
2022-04-23 18:03:00 【Xu Yeping】
Rust Multithread safe reference Arc
author : Xu Yeping
1 Arc And Rc Almost the same as , But multithreading is safe
Arc And Rc similar , The only difference is Arc Is multithreaded safe . See the example below :
use std::sync::Arc;
use std::thread;
fn main() {
let nums = Arc::new(vec![0, 1, 2, 3, 4]);
let mut childs = vec![];
for n in 0..5 {
let ns = nums.clone();
let c = thread::spawn(move || println!("{:?}", ns[n]));
childs.push(c);
}
for c in childs {
c.join().unwrap();
}
}
-------------------------------------------------------------------------------
>cargo run
0
2
4
1
3
2 Arc Can break the read-only spell
Arc、Rc Are read-only shares . however , This so-called read-only , Only at the grammatical level . We can construct syntactically read-only , But the data types that are actually allowed to be modified .Mutex Is such a reference type . Look at the code :
use std::sync::{
Arc, Mutex};
use std::thread;
fn main() {
let nums = Arc::new(Mutex::new(vec![]));
let mut childs = vec![];
for n in 0..5 {
let ns = nums.clone();
let c = thread::spawn(move || {
let mut v = ns.lock().unwrap();
v.push(n);
});
childs.push(c);
}
for c in childs {
c.join().unwrap();
}
println!("{:?}", nums);
}
-------------------------------------------------------------------------------
>cargo run
Mutex {
data: [0, 1, 3, 2, 4], poisoned: false, .. }
版权声明
本文为[Xu Yeping]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544498442.html
边栏推荐
- Re expression régulière
- Go的Gin框架学习
- C network related operations
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- Crawl the product data of Xiaomi Youpin app
- Halo 开源项目学习(二):实体类与数据表
- Map basemap Library
- Remember using Ali Font Icon Library for the first time
- Docker 安裝 Redis
- 2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
猜你喜欢
Go语言JSON包使用
Anchor location - how to set the distance between the anchor and the top of the page. The anchor is located and offset from the top
GDAL + ogr learning
k8s之实现redis一主多从动态扩缩容
Fashion classification case based on keras
Calculation of fishing net road density
Go对文件操作
Docker 安装 Redis
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
YOLOv4剪枝【附代码】
随机推荐
Fashion classification case based on keras
Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
The ultimate experience, the audio and video technology behind the tiktok
Selenium + webdriver + chrome realize Baidu to search for pictures
mysql自动启动设置用Systemctl start mysqld启动
Crack sliding verification code
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
20222 return to the workplace
Operators in C language
Use of list - addition, deletion, modification and query
SSD硬盘SATA接口和M.2接口区别(详细)总结
Implementation of k8s redis one master multi slave dynamic capacity expansion
Logic regression principle and code implementation
Map basemap Library
Encapsulate a timestamp to date method on string prototype
Welcome to the markdown editor
Timestamp to formatted date
Summary of floating point double precision, single precision and half precision knowledge
Clion installation tutorial
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader