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

  • 相关阅读:
    oracle在没
    一天中时针和分钟重合的次数
    oracle的隐藏的东东
    左右小移动
    JS全选的操作
    JS定时器
    在文件中查找字符串
    表单原件
    div和span互换
    div和span的区别
  • 原文地址:https://www.cnblogs.com/develon/p/14716656.html
Copyright © 2011-2022 走看看