当前位置:网站首页>rust多线程安全计数
rust多线程安全计数
2022-07-25 18:26:00 【pilaf1990】
每天进步一点点。
rust中用多个线程对同一个整数进行加数操作。为了保证线程安全,需要通过Mutex类型变量的lock方法进行加锁。
我们知道,Mutex类型变量也要遵循rust的所有权机制,如果想要在线程间共享Mutex的引用,需要通过Arc::new方法创建一个Mutex变量的原子引用计数器,然后再克隆多个Arc,通过闭包move到线程中。
相关源码如下:
use std::sync::{
Arc, Mutex};
use std::thread;
use std::thread::Builder;
fn main() {
// 在堆上分配一个i32整数0,通过智能指针Box将其绑定到number变量上
// 注意这儿无论是let number还是let mut number都可以,因为Mutex::new内部使用了UnsafeCell
let number = Box::new(0);
// 创建一个原子引用计数器counter
let counter = Arc::new(Mutex::new(number));
// 声明一个可变数组,里面存放线程的JoinHandle
let mut thread_handles = vec![];
// 从0到9
for i in 0..10 {
// 线程builder,设置了线程的名字是thread-i这种形式
let builder = Builder::new().name(format!("thread-{}", i));
// 每个线程都有一个对counter的引用
let thread_counter = Arc::clone(&counter);
// 创建并运行线程,通过move将thread_counter的所有权转移到闭包中
let handle = builder.spawn(move || {
println!("thread:{} begin to add 1..=100", thread::current().name().unwrap());
// 对每一个子线程,从1加到100,即5050
for j in 1..=100 {
// 加锁,并获取被保护的对象
let mut num = thread_counter.lock().unwrap();
// 执行加数字操作
*(*num) += j;
println!("{} add {} to number", thread::current().name().unwrap(), j);
// 代码块结束后,会释放掉锁
}
}).unwrap();
// 将线程的handle放到数组中,方便后续主线程对所有子线程进行join操作
thread_handles.push(handle);
}
for handle in thread_handles {
// 等待每一个子线程都完成
handle.join().unwrap();
}
// 输出最后的结果
println!("final result:{:#?}", counter.to_owned());
// 下面这行会编译报错,因为number已经被move到Mutex中了,受Mutex保护,一切对number的操作都要通过Mutex的保护,所以不能再直接访问number
// println!("final result:{:#?}", number);
}
程序运行的结果如下:
因为1+2+3+…+100=5050,10个线程分别加上5050就是50500,所以程序正确地使用了多个线程对整数进行加数操作。
边栏推荐
- Nc68 jumping steps
- Taishan Office Technology Lecture: conversion relations of inch, centimeter, pound, pika, Ti, line, word line and pixel
- R language uses GT package and gtextras package to display tabular data beautifully: GT_ bar_ Plot function and GT_ plt_ bar_ PCT function visual percentage bar graph, percentage bar graph of original
- SQL optimizer parsing | youth training camp notes
- Today's sleep quality record is 84 points
- [noi2015] package manager
- 7. 依赖注入
- NC78 反转链表
- CircleIndicator组件,使指示器风格更加多样化
- Use of C language cjson Library
猜你喜欢

1---电子实物认知

Express of nodejs simple example program

408 Chapter 2 linear table

C language -- 25 minesweeping game

Could not stop Cortex-M device! please check the JTAG cable的解决办法

Tkinter GUI address book management system

c语言---25 扫雷游戏

CircleIndicator组件,使指示器风格更加多样化

1--- electronic physical cognition

There was an error while marking a file for deletion
随机推荐
对角化、A的幂
Chapter 5 Basic Scripting: Shell Variables
数二2010真题考点
乐观锁解析
超全Mavan标签详解
程序的编译
[haoi2015] tree operation
Basic knowledge of documents
BL602 开发环境搭建
工程师必看的示波器探头安全使用说明书
Design practice of Netease strictly selecting inventory center
Construction of Huffman tree
SQL那些事
[QNX Hypervisor 2.2用户手册]9.5 dump
What are the methods of traversing arrays? What is the difference between the performance of the for loop foreach for/in for/of map and how to choose?
Taishan Office Technology Lecture: conversion relations of inch, centimeter, pound, pika, Ti, line, word line and pixel
imx6 RTL8189FTV移植
Compilation of program
Flexible current probe selection guide
R language ggplot2 visual line, custom configuration title text related content color and legend color match (match colors of groups)