I am trying to avoid race conditions in my code, and I am currently working with a single hyperthreaded CPU, so there are two logical CPUs, each with their own thread. If my understanding of hyperthreading is correct, these threads share the same resources, but their actions are actually time-sliced, not concurrent. Is it still possible for race conditions to emerge between these two hyperthreads?
For example, is there reason for me to change this:
Connection& connection = connections[num_connections];
... do some stuff
++num_connections;
to this:
Connection& connection = connections[num_connections++];
... do some stuff
It's possible.
If both threads execute a logic like
if(condition){
donate1MillionDollars
condition=false
}
even with time slicing, the two threads can enter the if block, and you become a lot poorer than expected :)
Without knowing what "do some stuff" implies, it's hard to answer specifically for your use case.