読者です 読者をやめる 読者になる 読者になる

Concurrency

rust

https://doc.rust-lang.org/book/concurrency.html

並列処理について。

use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

fn main() {
    let data = Arc::new(Mutex::new(vec![1, 2, 3]));

    for i in 0..3 {
        let data = data.clone();

        thread::spawn(move || {
            let mut data = data.lock().unwrap();
            data[0] += i;
        });
    }

    thread::sleep(Duration::from_millis(50));

    println!("{:?}", data);
}
$ cargo run
    Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/5_6_concurrency`
Mutex { data: [4, 2, 3] }