目次

Rustの勉強[並行性 その6]

ぎじゅつ

はじめに

#

NO IMAGEThe Rust Programming Language 日本語版 - The Rust Programming Language 日本語版
を読んでいる

  • 朝面倒なことがあったがメンタル関係なく勉強はしたい
    • ここの切り替えだいじにしたい

お勉強

#

NO IMAGE状態共有並行性 - The Rust Programming Language 日本語版
ここからやる

メモ

#
  • 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());
}
  • Rcで複数で参照できるように包む

再三、コンパイルし……別のエラーが出ました!コンパイラはいろんなことを教えてくれています。

$ cargo run
   Compiling shared-state v0.1.0 (file:///projects/shared-state)
error[E0277]: `Rc<Mutex<i32>>` cannot be sent between threads safely
(エラー: `Rc<Mutex<i32>>`はスレッド間で安全に送信できません)
  --> src/main.rs:11:36
   |
11 |         let handle = thread::spawn(move || {
   |                      ------------- ^------
   |                      |             |
   |  ____________________|_____________within this `{closure@src/main.rs:11:36: 11:43}`
   |                      |             (この`{[closure@src/main.rs:11:36: 11:43}`の中で)
   |                      |
   |                      required by a bound introduced by this call
   |                      (この呼び出しによって導入される境界によって必要とされます)
12 | |             let mut num = counter.lock().unwrap();
13 | |
14 | |             *num += 1;
15 | |         });
   | |_________^ `Rc<Mutex<i32>>` cannot be sent between threads safely
   |              (`Rc<Mutex<i32>>`はスレッド間で安全に送信できません)
   |
   = help: within `{closure@src/main.rs:11:36: 11:43}`, the trait `Send` is not implemented for `Rc<Mutex<i32>>`
   =(ヘルプ: `{closure@src/main.rs:11:36: 11:43}`の中で、トレイト`Send`は`Rc<Mutex<i32>>`に対して実装されていません
note: required because it's used within this closure
(注釈: このクロージャの中で使用されているので、要求されます)
  --> src/main.rs:11:36
   |
11 |         let handle = thread::spawn(move || {
   |                                    ^^^^^^^
note: required by a bound in `spawn`
(注釈: `spawn`の境界によって要求されます)
  --> /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/thread/mod.rs:678:1

For more information about this error, try `rustc --explain E0277`.
error: could not compile `shared-state` (bin "shared-state") due to 1 previous error
  • マジか

残念ながら、Rc<T>はスレッド間で共有するには安全ではないのです。

  • ふざけんな

    • そういえばシングルスレッド専用って書いてあったじゃんか
  • Arcがあるらしい

    • なるほど、これが説明したかったのか

スレッド安全性が、本当に必要な時だけ支払いたいパフォーマンスの犠牲とともに得られるものだからです。

  • まぁそうだよねー

まとめ

#
  • Arcがあることを証明するために長々説明されただけだった

NO IMAGE状態共有並行性 - The Rust Programming Language 日本語版
次はここ