Question regarding global memory write protection

Just a simple question about a discussion a friend and I were having earlier. If two separate threads attempt to write identical values to the same variable in global memory at the same time, what happens? Does one of the threads wait until it can have access to the variable or does it attempt to access the variable and then move to the next line of code when it can’t? Or does something more complex happen?

In case of conflicts in writing to memory, (ie. more than 1 thread trying to write to same location, at any given time) only one of the threads is guaranteed to succeed. Since this thread succeeds, it happily moves on with it’s execution. However, all other threads which were denied the access to memory, because this thread was writing to memory, will, in the next clock cycle, again try to attempt to write to memory. Now, one of them will succeed and the remaining threads will again try in the next clock cycle… so on… until all the threads have successfully written to memory.

Note that even though all the threads will eventually succeed in writing to that location, the order will never be deterministic. Now, considering the worst case (which in fact generally happens) with all threads trying to write a different value to the same location, you can never exactly determine, beforehand, what could be the final value in the location.

PS: Writing such a code with race condition is hazardous to both the programmer and the user, and should ALWAYS be avoided!!