Concurrent writes to array with unspecified (but not UB) outcome?

⚓ rust    📅 2025-04-26    👤 surdeus    👁️ 1      

surdeus

I have an unusual question. I want to know if there is any mechanism or data structure I could implement that would let multiple threads efficiently/quickly write to the same array concurrently without triggering UB.

I understand that data races are always UB, and I obviously want to avoid that. What I'm trying to figure out is if there's any way to avoid data races that does not impact the performance "too much". Something involving atomics or some kind of efficient queue and dedicated writer thread?

I accept that the written value may be completely unpredictable and unspecified if multiple threads tried to write to the same location.

Context: I'm thinking about this problem because I want to implement some realtime software rendering techniques that don't need to have a 100% "correct" output (demoscene stuff), but it is important that it's fast and doesn't crash/even worse due to UB. Some similar techniques exist where GPU compute shaders write to a shared buffer in an unpredictable way.

2 posts - 2 participants

Read full topic

🏷️ rust_feed