Safe multi-threaded mutating buffer without locks

Published 410d ago

 ayebear

This is from @ilyvion on the Rust programming language discord!

fn main() {
    let mut array = [0; 10];
    std::thread::scope(|s| {
        for (i, e) in array.iter_mut().enumerate() {
            s.spawn(move || {
                *e = i * 2;
            });
        }
    });

    println!("{array:?}");
}

3

Please login or sign up to comment and collaborate