I'm going through some lecture notes with code like this and don't really understand why disabling interrupts solves the concurrency problem. If there are 2 threads running, the first one calls CLI() to disable interrupts, then enters the critical section. How does this stop the second thread from calling increment(x), calling CLI(), then also entering the critical section?
There seems to be some gap in my understanding - is it that an interrupt is required for the second thread to run? When would the CPU switch to running a different thread?
void increment(int &x) {
CLI();
// critical section
int temp = x;
temp = temp + 1;
x = temp;
STI();
}