首页 > 其他 > 详细

rust: Typical multi thread patterns

时间:2020-03-08 12:29:05      阅读:84      评论:0      收藏:0      [点我收藏+]
use std::sync::Mutex;
use std::sync::Arc;
use std::{thread};

fn main() {
    // Typical multi thread patterns
    let data = Arc::new(Mutex::new(vec![1u32, 2, 3]));
    let mut handles = vec![];

    for i in 0..3 {
        let data = data.clone();
        let handle = thread::spawn(move || {
            let mut datax = data.lock().unwrap();
            datax[i] += 1;
        });
        handles.push(handle);
    }

    for handle in handles {
        handle.join().unwrap();
    }
}

rust: Typical multi thread patterns

原文:https://www.cnblogs.com/cutepig/p/12441472.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!