当前位置:   article > 正文

RUST——互斥锁的使用_rust mutex

rust mutex

互斥锁是Rust语言中所提供的,基于共享内存的方式来实现线程之间通信的机制。

Mutex的简单使用

Mutex是rust标准库中提供的互斥锁的实现。使用的示例代码如下所示:

  1. use std::sync::Mutex;
  2. fn main() {
  3. let m = Mutex::new(5);
  4. {
  5. let mut num = m.lock().unwrap();
  6. *num = 6;
  7. }
  8. println!("m = {:?}", m);
  9. }

共享变量是存储于Mutex对象的内部的,如果要访问共享的数据,需要先获取锁,然后才能访问内部的数据。

Mutex在多线程之间的共享

下面我们尝试在多个线程之间共享Mutex对象,以实现对数据的共享。示例代码如下:

  1. use std::sync::Mutex;
  2. use std::thread;
  3. fn main() {
  4. let counter = Mutex::new(0);
  5. let mut handles = vec![];
  6. for _ in 0..10 {
  7. let handle = thread::spawn(move || {
  8. let mut num = counter.lock().unwrap();
  9. *num += 1;
  10. });
  11. handles.push(handle);
  12. }
  13. for handle in handles {
  14. handle.join().unwrap();
  15. }
  16. println!("Result: {}", *counter.lock().unwrap());
  17. }

如果我们编译上述代码,会得到如下的结果:

  1. Compiling playground v0.0.1 (/playground)
  2. error[E0382]: use of moved value: `counter`
  3. --> src/main.rs:9:36
  4. |
  5. 5 | let counter = Mutex::new(0);
  6. | ------- move occurs because `counter` has type `Mutex<i32>`, which does not implement the `Copy` trait
  7. ...
  8. 9 | let handle = thread::spawn(move || {
  9. | ^^^^^^^ value moved into closure here, in previous iteration of loop
  10. 10 | let mut num = counter.lock().unwrap();
  11. | ------- use occurs due to use in closure
  12. For more information about this error, try `rustc --explain E0382`.
  13. error: could not compile `playground` due to previous error

显然,在创建线程的时候,使用move语义,Mutex对象的所有权发生了转移,因而无法实现在多个线程之间共享Mutex的对象。如果想要实现Mutex对象在多个线程之间共享,必须使用具有多所有权的对象,我们很自然的就想到了rust中基于引用计数实现的智能指针Rc。

基于Rc实现Mutex在多线程之间的共享

基于Rc的机制实现Mutex在多线程之间的共享的示例代码如下:

  1. use std::rc::Rc;
  2. use std::sync::Mutex;
  3. use std::thread;
  4. fn main() {
  5. let counter = Rc::new(Mutex::new(0));
  6. let mut handles = vec![];
  7. for _ in 0..10 {
  8. let counter = Rc::clone(&counter);
  9. let handle = thread::spawn(move || {
  10. let mut num = counter.lock().unwrap();
  11. *num += 1;
  12. });
  13. handles.push(handle);
  14. }
  15. for handle in handles {
  16. handle.join().unwrap();
  17. }
  18. println!("Result: {}", *counter.lock().unwrap());
  19. }

如果我们编译上述代码,会得到如下的编译结果:

  1. Compiling playground v0.0.1 (/playground)
  2. error[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely
  3. --> src/main.rs:11:22
  4. |
  5. 11 | let handle = thread::spawn(move || {
  6. | ______________________^^^^^^^^^^^^^_-
  7. | | |
  8. | | `Rc<Mutex<i32>>` cannot be sent between threads safely
  9. 12 | | let mut num = counter.lock().unwrap();
  10. 13 | |
  11. 14 | | *num += 1;
  12. 15 | | });
  13. | |_________- within this `[closure@src/main.rs:11:36: 15:10]`
  14. |
  15. = help: within `[closure@src/main.rs:11:36: 15:10]`, the trait `Send` is not implemented for `Rc<Mutex<i32>>`
  16. = note: required because it appears within the type `[closure@src/main.rs:11:36: 15:10]`
  17. note: required by a bound in `spawn`
  18. For more information about this error, try `rustc --explain E0277`.
  19. error: could not compile `playground` due to previous error

从上述结果中,我们可以看到,Rc的实现的引用计数,并不是线程安全的,无法在多线程之间传递。很自然地,我们可以使用rust中的线程安全的引用计数实现机制Arc,A代表的是Atomic,是原子性的操作,因而可以保证线程的安全。

基于Arc实现Mutex在多线程之间的共享

基于Arc实现的引用计数,来实现在Mutex在多线程之间的共享的示例代码如下:

  1. use std::sync::{Arc, Mutex};
  2. use std::thread;
  3. fn main() {
  4. let counter = Arc::new(Mutex::new(0));
  5. let mut handles = vec![];
  6. for _ in 0..10 {
  7. let counter = Arc::clone(&counter);
  8. let handle = thread::spawn(move || {
  9. let mut num = counter.lock().unwrap();
  10. *num += 1;
  11. });
  12. handles.push(handle);
  13. }
  14. for handle in handles {
  15. handle.join().unwrap();
  16. }
  17. println!("Result: {}", *counter.lock().unwrap());
  18. }

运行上述代码,结果如下:

Result: 10

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/847177
推荐阅读
  

闽ICP备14008679号