zoukankan      html  css  js  c++  java
  • Rust中的并发

    std::thread

    use std::{thread::{self, JoinHandle, sleep}, time::Duration};
    
    fn main() -> std::io::Result<()> {
        let jh: JoinHandle<i32> = thread::spawn(|| {
            sleep(Duration::from_millis(3000));
            88
        });
        let a: i32 = jh.join().unwrap();
        println!("Hello, world! {}", a);
        Ok(())
    }
    

    sync::mpsc::channel

    use std::{sync::mpsc::channel, thread::sleep, time::Duration};
    use std::thread;
    
    fn main() -> () {
        let (sender, receiver) = channel();
    
        // Spawn off an expensive computation
        // 产生昂贵的计算
        thread::spawn(move || {
            sender.send(get_result()).unwrap();
        });
    
        // Do some useful work for awhile
        // 暂时做一些有用的工作
    
        // Let's see what that answer was
        // 让我们看看答案是什么
        println!("{:?}", receiver.recv().unwrap());
    }
    
    fn get_result() -> i32 {
        sleep(Duration::from_millis(3000));
        88
    }
    

    END

  • 相关阅读:
    docker (2) 私有仓库的建立
    golang (5) http 请求分析
    java (1)
    golang (5) ---工程管理
    Mac使用一些经验
    数组的遍历
    进制
    数组的初始化
    数组基本概念
    博客开始更新第一天
  • 原文地址:https://www.cnblogs.com/develon/p/14716656.html
Copyright © 2011-2022 走看看