I'm having some trouble trying to figure out how to share mutable data between threads.
I have a Vec I want multiple threads to write to simultaneously. I don't care if the threads tries to write to the same index (data races are allowed in this context).
How do I go about doing this?
pcpthm:
No, it is never allowed. Rust explicitly says that any data race 2 is an instant UB (undefined behavior). You have to use an atomic operation even if the result doesn't matter.
I don't know what you want to achieve but here is an example of sharing a Vec between threads and threads "race" (but not a data race) for a write:
mbrubeck:
Atomic operations also inhibit compiler optimizations that would result in incorrect code in the presence of data races. This means that they can have performance impact and are necessary for soundness and correctness even if you compile only on platforms where the atomic reads/writes use the same instructions as non-atomic ones.
More detail: https://users.rust-lang.org/t/write-to-shared-memory-from-multiple-threads/34587