RUST——互斥锁的使用
作者:互联网
互斥锁是Rust语言中所提供的,基于共享内存的方式来实现线程之间通信的机制。
Mutex的简单使用
Mutex是rust标准库中提供的互斥锁的实现。使用的示例代码如下所示:
use std::sync::Mutex;
fn main() {
let m = Mutex::new(5);
{
let mut num = m.lock().unwrap();
*num = 6;
}
println!("m = {:?}", m);
}
共享变量是存储于Mutex对象的内部的,如果要访问共享的数据,需要先获取锁,然后才能访问内部的数据。
Mutex在多线程之间的共享
下面我们尝试在多个线程之间共享Mutex对象,以实现对数据的共享。示例代码如下:
use std::sync::Mutex;
use std::thread;
fn main() {
let counter = Mutex::new(0);
let mut handles = vec![];
for _ in 0..10 {
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", *counter.lock().unwrap());
}
如果我们编译上述代码,会得到如下的结果:
Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `counter`
--> src/main.rs:9:36
|
5 | let counter = Mutex::new(0);
| ------- move occurs because `counter` has type `Mutex<i32>`, which does not implement the `Copy` trait
...
9 | let handle = thread::spawn(move || {
| ^^^^^^^ value moved into closure here, in previous iteration of loop
10 | let mut num = counter.lock().unwrap();
| ------- use occurs due to use in closure
For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` due to previous error
显然,在创建线程的时候,使用move语义,Mutex对象的所有权发生了转移,因而无法实现在多个线程之间共享Mutex的对象。如果想要实现Mutex对象在多个线程之间共享,必须使用具有多所有权的对象,我们很自然的就想到了rust中基于引用计数实现的智能指针Rc。
基于Rc实现Mutex在多线程之间的共享
基于Rc的机制实现Mutex在多线程之间的共享的示例代码如下:
use std::rc::Rc;
use std::sync::Mutex;
use std::thread;
fn main() {
let counter = Rc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter = Rc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", *counter.lock().unwrap());
}
如果我们编译上述代码,会得到如下的编译结果:
Compiling playground v0.0.1 (/playground)
error[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely
--> src/main.rs:11:22
|
11 | let handle = thread::spawn(move || {
| ______________________^^^^^^^^^^^^^_-
| | |
| | `Rc<Mutex<i32>>` cannot be sent between threads safely
12 | | let mut num = counter.lock().unwrap();
13 | |
14 | | *num += 1;
15 | | });
| |_________- within this `[closure@src/main.rs:11:36: 15:10]`
|
= help: within `[closure@src/main.rs:11:36: 15:10]`, the trait `Send` is not implemented for `Rc<Mutex<i32>>`
= note: required because it appears within the type `[closure@src/main.rs:11:36: 15:10]`
note: required by a bound in `spawn`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
从上述结果中,我们可以看到,Rc的实现的引用计数,并不是线程安全的,无法在多线程之间传递。很自然地,我们可以使用rust中的线程安全的引用计数实现机制Arc,A代表的是Atomic,是原子性的操作,因而可以保证线程的安全。
基于Arc实现Mutex在多线程之间的共享
基于Arc实现的引用计数,来实现在Mutex在多线程之间的共享的示例代码如下:
use std::sync::{Arc, Mutex};
use std::thread;
fn main() {
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter = Arc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
println!("Result: {}", *counter.lock().unwrap());
}
运行上述代码,结果如下:
Result: 10
标签:handle,unwrap,counter,互斥,num,let,使用,Mutex,RUST 来源: https://blog.csdn.net/z974656361/article/details/122508749