Hacker News new | ask | show | jobs
by RasmusWL 3741 days ago
Can someone enlighten me as to why the first snippet has a data race? Won't the resulting array become [2,3,4]?

    let mut data = vec![1, 2, 3];

    for i in 0..3 {
        thread::spawn(move || {
            data[i] += 1;
        });
    }
2 comments

I don't think there's a data race there, but the compiler can't check that. What the compiler sees is more than one thread accessing the variable `data`, which could cause a data race.