2 ms·
If the loop uses loop_variable within its body this can cause a data race. Imagine: for (...) { int x = array[loops[0].loop_variable]; arra
by progbits 3y ago
If the loop uses loop_variable within its body this can cause a data race. Imagine:
for (...)
{
int x = array[loops[0].loop_variable];
array[loops[0].loop_variable] = calculation(x);
}
If the loop_variable = limit from another thread happens after the array read but before the write you get corrupted answer, not just incomplete one.
How do you work around that? Storing a copy and only using that? Or does it not matter in your usecases?
- samsquire 3y agoI never used the loop variable inside a loop in my testing but thank you for detecting this! I would change it to this, shouldn't slow down the loop either. for (int i = 0; loops[0].loop_variable < loops[0].limit ; i = loops[0].loop_variable, loops[0].loop_variable++) { // Hot loop here }