Whether you are a C or C++ programmer or work in another discipline, understanding these concepts is important for your work and deliverables. Locking and synchronization confuse many programmers. In this short post, I’ll explain some of these concepts in simple terms.

The race condition

  • A race condition is when two or more threads of execution compete for the same resource, like shared memory, and try to access and modify the memory, and the final result depends on which thread gets there first.
  • It is as if the threads are racing for that resource or memory.
  • Let’s take an example where two threads share the same counter, i, and each tries to increment it by one.
Diagram showing thread 1 and thread 2 both updating a shared variable i to 0.
Two threads trying to access and modify the same memory location
  • Each thread updates the counter in three separate steps.
    • Read the shared counter into a register.
    • Add one to the value in the register.
    • Write the result back to the shared counter.
  • These steps may interleave, and one may overwrite the result of the other.

Incorrect state of shared data

Diagram illustrating a race condition: two threads read i = 0, increment it, and both write i = 1 instead of the expected final value of 2.

Critical section

  • If a piece of code can be executed by two or more competing threads and has the potential to access and modify shared memory or some other shared resource and leave it in an inconsistent state, then that piece of code is regarded as a critical section.
  • In our example, ++i accesses and modifies a shared memory location used by two threads.
  • This part of the code can lead to a race condition between two or more competing threads and can leave the shared variable in an incorrect state.
  • Therefore, ++i is a critical section.

Locking

  • Locking is simply a way to make the competing threads follow a rule and prevent them from stepping into each other’s execution of the critical section.
  • The simple rule is there is a special type of variable which has value 0 or 1 and we call it a lock. It is provided by the operating system and also managed by the OS.
  • When a thread of execution wants to enter the critical section, it requests the lock.
  • The operating system checks the lock value, and if it is 1, that means the lock is available and the thread is allowed to take the lock. The lock is decremented by 1; hence, the value is 0 after that. The check and the decrement happen as one atomic step, so two threads can never both see the value 1.
  • While thread 1 is holding the lock, if another thread comes and wants to enter the critical section, it must request the lock.
  • The OS checks if the lock is available, and as it is not available, the requesting thread has to wait until the lock becomes available again. In Linux, it is put in a wait queue and does not use the CPU while it waits.
  • Once thread 1 is done executing the critical section, it must release (unlock) the lock, and the OS wakes up a waiting thread so that it can take the lock and enter the critical section.
  • All threads that are competing to enter the critical section must abide by this rule.

Locking Steps 1 & 2 Contention

Two-step diagram showing two threads incrementing a shared variable and using a lock to prevent inconsistent memory state.

Locking Step 3 – controlled access

Diagram illustrating thread synchronization with a lock: thread 1 enters the critical section, thread 2 waits, then acquires the lock after thread 1 releases it.

Discover more from Tech For Talk

Subscribe to get the latest posts sent to your email.

Leave a Reply