Thread scope



examples/threads/thread-scope/src/main.rs
macro_rules! prt {
    ($text: expr, $var: expr) => {
        println!("{:11} {:?} {:p} {:?}", $text, $var, &$var, $var.as_ptr());
    };
}

fn main() {
    let animals = vec![
        String::from("crab"),
        String::from("ant"),
        String::from("cat"),
        String::from("dog"),
        String::from("bat"),
    ];

    prt!("Before:", animals);

    std::thread::scope(|scope| {
        scope.spawn(|| list_animals(&animals));
        scope.spawn(|| list_animals(&animals));
        scope.spawn(|| list_animals(&animals));
    });

    prt!("After:", animals);
}

fn list_animals(animals: &Vec<String>) {
    // Enable this to show that they work in parallel
    // for animal in animals {
    //     println!(" {} in {:?}", animal, std::thread::current().id());
    //     std::thread::sleep(std::time::Duration::from_millis(rand::random::<u8>() as u64));
    // }

    prt!(format!("{:?}", std::thread::current().id()), animals);
}

Before:     ["crab", "ant", "cat", "dog", "bat"] 0x7ffc77a51ef8 0x59798b84f480
ThreadId(2) ["crab", "ant", "cat", "dog", "bat"] 0x76e603fff688 0x59798b84f480
ThreadId(4) ["crab", "ant", "cat", "dog", "bat"] 0x76e6037ff688 0x59798b84f480
ThreadId(3) ["crab", "ant", "cat", "dog", "bat"] 0x76e603bff688 0x59798b84f480
After:      ["crab", "ant", "cat", "dog", "bat"] 0x7ffc77a51ef8 0x59798b84f480