当前位置:网站首页>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
边栏推荐
- C byte array (byte []) and string are converted to each other
- Open source key component multi_ Button use, including test engineering
- MySQL 中的字符串函数
- Climbing watermelon video URL
- Jenkspy package installation
- _ FindText error
- ROS package NMEA_ navsat_ Driver reads GPS and Beidou Positioning Information Notes
- Utilisation de la liste - Ajouter, supprimer et modifier la requête
- Eigen learning summary
- Oil monkey website address
猜你喜欢
MySQL_ 01_ Simple data retrieval
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
Transfer learning of five categories of pictures based on VGg
Docker 安装 Redis
ArcGIS license error -15 solution
MySQL auto start settings start with systemctl start mysqld
String function in MySQL
Data stream encryption and decryption of C
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
Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
随机推荐
ROS package NMEA_ navsat_ Driver reads GPS and Beidou Positioning Information Notes
MySQL_01_简单数据检索
_ FindText error
Halo 开源项目学习(二):实体类与数据表
Docker 安裝 Redis
C language to achieve 2048 small game direction merging logic
Timestamp to formatted date
Svn simple operation command
An example of linear regression based on tensorflow
Realsense selection comparison d455 d435i d415 t265 3D hardware comparison
C1 notes [task training chapter I]
Map basemap Library
Calculation of fishing net road density
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
C [file operation] read TXT text by line
Read excel, int digital time to time
Auto.js 自定义对话框
Basic usage of crawler requests
YOLOv4剪枝【附代码】
Cross domain settings of Chrome browser -- including new and old versions