prevent race condition without using locks C++
- by Hristo
How do I prevent a race condition with locking or using mutexes/semaphors in C++? I'm dealing with a nested for loop in which I will be setting a value in an array:
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < o; ++k)
array[k] += foo(...);
More or less, I want to deal with this so that I can ensure different threads running at the same time don't write to array[k] at the same time. Any suggestions on how to approach this?
Thanks,
Hristo