<repo_name>pxfnc/user-thread<file_sep>/README.md
# user-thread

スレッド作ってみたり、スレッドの中でスレッド作ってみたりとかいろいろやってみた

## run
```
cargo run --release
```<file_sep>/src/main.rs
use std::mem::replace;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;

fn main() {
    println!("start");

    let t3: Arc<Mutex<Option<JoinHandle<()>>>> = Arc::new(Mutex::new(None));
    let t3_for_t1 = t3.clone();

    let t1 = thread::spawn(move || {
        println!("t1 start");
        thread::sleep(Duration::from_secs(2));
        {
            t3_for_t1
                .lock()
                .map(|mut v| {
                    let t3 = thread::spawn(|| {
                        println!("t3 start");
                        thread::sleep(Duration::from_secs(4));
                        println!("t3 done");
                    });
                    *v = Some(t3);
                })
                .unwrap_or_default();
        };
        thread::sleep(Duration::from_secs(6));
        println!("t1 done");
    });

    let t2 = thread::spawn(|| {
        println!("t2 start");
        thread::sleep(Duration::from_secs(4));
        // panic!("t2 panic");
        println!("t2 done");
    });
    t1.join().unwrap_or_default();
    t2.join().unwrap_or_default();
    t3.lock()
        .ok()
        .and_then(|mut t3_| replace(&mut *t3_, None).and_then(|t3| t3.join().ok()))
        .unwrap_or_default();
}
