Function understand_local_thread

Source
pub fn understand_local_thread()
Expand description

§理解本地线程,理解并发

  • 并发:同时「应对」很多事的能力
  • 并行:同时「执行」很多事的能力

相关类型:

use std::thread;

fn main() {
    // Duration 实现了 Copy、Send、Sync
    let duration = std::time::Duration::from_millis(3000);

    println!("Main thread");

    let handle  = thread::spawn(move || {
        println!("Sub thread 1");

        // 注意:它的父线程是主线程,而不是线程1
        let handle2 = thread::spawn( move || {
            println!("Sub thread 2");
            thread::sleep(duration);
        });

        handle2.join().unwrap();
        thread::sleep(duration);
    });

    handle.join().unwrap();
    thread::sleep(duration);
}