Race condition?


can someone explain to me why in the following case an atomicAdd is needed?

My kernel does a certain check on an array, the index of which is determined by the thread ID. Depending on the result of the check,
it sets a global (device) variable “update” to 1, otherwise it leaves it untouched.

Like this:

if (condition depending on array data) update=1;

So it is possible that multiple threads find the same condition fulfilled, and hence they may simultaneously set “update” to 1. But that shouldn’t matter once it has been updated at least once. However this seems to be a race condition, since sometimes after reading out “update” from the host code, it is 0 when it should actually be 1. Using atomicAdd(&updatenl,1) in the kernel solves this problem.

Can anybody enlighten me on the type of mistake I made?

kind regards,