当前位置:网站首页>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
边栏推荐
- How to install jsonpath package
- MySQL auto start settings start with systemctl start mysqld
- Crawler for querying nicknames and avatars based on qqwebapi
- C language to achieve 2048 small game direction merging logic
- SSD硬盘SATA接口和M.2接口区别(详细)总结
- 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
- What are the relationships and differences between threads and processes
- Welcome to the markdown editor
- Re regular expression
- 20222 return to the workplace
猜你喜欢
Docker 安裝 Redis
Romance in C language
2022 tea artist (primary) examination simulated 100 questions and simulated examination
极致体验,揭晓抖音背后的音视频技术
Operators in C language
GDAL + ogr learning
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
String function in MySQL
.105Location
Nat commun | current progress and open challenges of applied deep learning in Bioscience
随机推荐
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
Thirteen documents in software engineering
Process management command
Re regular expression
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
C1 notes [task training chapter I]
C# 网络相关操作
Solving the problem of displaying too many unique values in ArcGIS partition statistics failed
Crawl the product data of Xiaomi Youpin app
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
Open source key component multi_ Button use, including test engineering
[UDS unified diagnostic service] IV. typical diagnostic service (4) - online programming function unit (0x34-0x38)
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
A few lines of code teach you to crawl lol skin pictures
C network related operations
QTableWidget使用讲解
JS high frequency interview questions
Using files to save data (C language)
ES6 face test questions (reference documents)
Svn simple operation command