Lecture Topic: Producer Consumer

There is a problem with using a counter in a multithreaded queue, as the counter can be stored in the CPU's registers at the same time, and when saving that register with changes, with out of order execution, can lead to misleading counts.

Say a CPU loads a variable into a register and on two threads, one increments it, and the other decrements it, you can encounter a scenario where it saves the the variable as not the intended value. This is called a race condition.

You need some for of synchronization mechanism to ensure this operation occurs correctly

You may also need a synchronization mechanism for scenarios where only 1 thread reads and 1 thread writes. An example is an ATM, where a read reads a valid valance, and another separate transaction occurs, and in the time between the start and finalization of the purchase, there is now insufficient funds.

Critical Section Problem:

A typical example of a critical section problem that, in a loop, there is some critical section is reading shared memory.