Is there a race condition for multiple threads on multiple cores trying to lock a global variable?

by Chowza   Last Updated September 02, 2018 19:05 PM

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?



Related Questions


How Semaphore Signal Works

Updated July 13, 2017 08:05 AM

Why is a semaphore called a semaphore?

Updated September 25, 2017 02:05 AM


Injecting locking policy into a class

Updated June 14, 2015 23:02 PM

Why should I know concurrent programming?

Updated May 12, 2016 08:02 AM