当前位置:网站首页>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# 的数据流加密与解密
- Batch export ArcGIS attribute table
- SSD硬盘SATA接口和M.2接口区别(详细)总结
- word frequency count
- C1 notes [task training part 2]
- Crawling mobile game website game details and comments (MQ + multithreading)
- An example of linear regression based on tensorflow
- 2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
- Basic usage of crawler requests
- C byte array (byte []) and string are converted to each other
猜你喜欢
Go file operation
.104History
Install pyshp Library
2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
A few lines of code teach you to crawl lol skin pictures
YOLOv4剪枝【附代码】
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
Implementation of image recognition code based on VGg convolutional neural network
Docker 安裝 Redis
.105Location
随机推荐
Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
MySQL_01_简单数据检索
Cross domain settings of Chrome browser -- including new and old versions
cartographer_ There is no problem compiling node, but running the bug that hangs directly
C byte array (byte []) and string are converted to each other
C#字节数组(byte[])和字符串相互转换
Map basemap Library
Go language JSON package usage
Gobang game based on pyGame Library
Build openstack platform
Generate verification code
解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
Crack sliding verification code
极致体验,揭晓抖音背后的音视频技术
How to install jsonpath package
Selenium + webdriver + chrome realize Baidu to search for pictures
Visualization of residential house prices
Go的Gin框架学习
云原生虚拟化:基于 Kubevirt 构建边缘计算实例