I understand in a single core system, multiple threads operate in sequence, scheduled by the OS. Hence it can't be possible to have a race condition since by definition they are taking turns locking a variable.
However if I have a multicore processor, is it possible for multiple threads of the same process to be run concurrently and hence both attempt to lock a variable at the same time?
For example I have a global semaphore s = 1
If 2 threads on different cores ran simultaneously, they both would try to decrement the semaphore and be successful in race conditions.
In a single core, 2 threads operate in sequence (scheduled by OS), so this is not possible, as the second thread will see s = 0 by the time it is scheduled.
Therefore I'm wondering if I have a process that spawns multiple threads, and I have multiple cores, what is stopping the race condition where 2 threads running simultaneously see the global semaphore as s=1 at the same time and attempt to operate on it?