3 ms·
Yes that's right. I think as the other commentator said that you would need to use signals to interrupt within between elements of a loop. I try not interrupt
by samsquire 3y ago
Yes that's right.
I think as the other commentator said that you would need to use signals to interrupt within between elements of a loop.
I try not interrupt a hot for loop or while true loop to reduce performance impact of an if statement in a loop.
register_loop(loops, 0, 1000000);
for (; loops[0].loop_variable < loops[0].limit ; loops[0].loop_variable++)
{
// Hot loop here
}
It's difficult from another thread to know where in the loop another thread is in.
Then from another thread
loops[0].loop_variable = loops[0].limit
- progbits 3y agoIf 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 }