zoukankan      html  css  js  c++  java
  • rust thread

    https://kaisery.github.io/trpl-zh-cn/ch16-01-threads.html

    use std::thread;
    use std::time::Duration;
    
    fn main() {
        thread::spawn(|| {
            for i in 1..10 {
                println!("hi number {} from the spawned thread!", i);
                thread::sleep(Duration::from_millis(1));
            }
        });
    
        for i in 1..5 {
            println!("hi number {} from the main thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    }
       Compiling hello_world v0.1.0 (/data2/rust/thread)
        Finished dev [unoptimized + debuginfo] target(s) in 0.59s
    [root@bogon thread]# cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.01s
         Running `target/debug/hello_world`
    hi number 1 from the main thread!
    hi number 1 from the spawned thread!
    hi number 2 from the main thread!
    hi number 2 from the spawned thread!
    hi number 3 from the main thread!
    hi number 3 from the spawned thread!
    hi number 4 from the main thread!
    hi number 4 from the spawned thread!
    use std::thread;
    use std::time::Duration;
    
    fn main() {
        let handle = thread::spawn(|| {
            for i in 1..10 {
                println!("hi number {} from the spawned thread!", i);
                thread::sleep(Duration::from_millis(1));
            }
        });
    
        for i in 1..5 {
            println!("hi number {} from the main thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    
        handle.join().unwrap();
    }
    [root@bogon thread1]# cargo build
       Compiling hello_world v0.1.0 (/data2/rust/thread1)
        Finished dev [unoptimized + debuginfo] target(s) in 0.58s
    [root@bogon thread1]# cargo run
        Finished dev [unoptimized + debuginfo] target(s) in 0.01s
         Running `target/debug/hello_world`
    hi number 1 from the main thread!
    hi number 1 from the spawned thread!
    hi number 2 from the main thread!
    hi number 2 from the spawned thread!
    hi number 3 from the main thread!
    hi number 3 from the spawned thread!
    hi number 4 from the main thread!
    hi number 4 from the spawned thread!
    hi number 5 from the spawned thread!
    hi number 6 from the spawned thread!
    hi number 7 from the spawned thread!
    hi number 8 from the spawned thread!
    hi number 9 from the spawned thread!

    这两个线程仍然会交替执行,不过主线程会由于 handle.join() 调用会等待直到新建线程执行完毕。

    不过让我们看看将 handle.join() 移动到 main 中 for 循环之前会发生什么,如下:

    文件名: src/main.rs

     
    use std::thread;
    use std::time::Duration;
    
    fn main() {
        let handle = thread::spawn(|| {
            for i in 1..10 {
                println!("hi number {} from the spawned thread!", i);
                thread::sleep(Duration::from_millis(1));
            }
        });
    
        handle.join().unwrap();
    
        for i in 1..5 {
            println!("hi number {} from the main thread!", i);
            thread::sleep(Duration::from_millis(1));
        }
    }
    

    主线程会等待直到新建线程执行完毕之后才开始执行 for 循环,所以输出将不会交替出现,如下所示:

     
    hi number 1 from the spawned thread!
    hi number 2 from the spawned thread!
    hi number 3 from the spawned thread!
    hi number 4 from the spawned thread!
    hi number 5 from the spawned thread!
    hi number 6 from the spawned thread!
    hi number 7 from the spawned thread!
    hi number 8 from the spawned thread!
    hi number 9 from the spawned thread!
    hi number 1 from the main thread!
    hi number 2 from the main thread!
    hi number 3 from the main thread!
    hi number 4 from the main thread!
    

    诸如将 join 放置于何处这样的小细节,会影响线程是否同时运行。

  • 相关阅读:
    无线网络安全标准是什么?漏洞是怎样产生的
    揭秘英特尔处理器如何启动?
    数据包级网络遥测和网络安全推送分析
    自动化与网络工程之间的关系
    ElementUI中的el-table中实现动态添加一行、删除一行、清空所有行
    ElementUI中的el-table实现多选框不勾选的提示
    Nodejs中的fs模块的使用
    ElementUI中的el-table怎样实现多选与单选
    MyBatis中针对if-test的参数为指定值的xml写法
    SqlServer中怎样从Excel中导入数据
  • 原文地址:https://www.cnblogs.com/dream397/p/14189581.html
Copyright © 2011-2022 走看看