3 ms·
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
by RasmusWL 11y 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;
});
}
- gsjs 11y agoI 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.
- Manishearth 11y agoThat's a mistake, clarified: https://github.com/rust-lang/rust/pull/32538 https://github.com/rust-lang/rust/pull/32538