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 放置于何处这样的小细节,会影响线程是否同时运行。

  • 相关阅读:
    窥探算法之美妙——详细讲解寻找最长重复字符串的原理
    窥探算法之美妙——寻找数组中最小的K个数&python中巧用最大堆
    窥探算法之美妙——统计整数二进制中1的个数
    第一次向开源项目贡献代码的历程
    编写高质量代码--改善python程序的建议(八)
    Mysql数据类型TINYINT(1)与BOOLEAN踩坑记
    Mysql Hash索引和B-Tree索引区别(Comparison of B-Tree and Hash Indexes)
    详解计算机中的Byte、bit、字、字长、字节
    什么是不忘初心
    最简单的JS实现json转csv
  • 原文地址:https://www.cnblogs.com/dream397/p/14189581.html
Copyright © 2011-2022 走看看