liveBook · Manning

Critical region

As explained in Grokking Concurrency

A critical section is a portion of program execution that accesses shared data or resources and must be protected from conflicting concurrent access.

A critical section is code that may run concurrently and accesses shared data or a resource. Like a printer that should finish one file before starting another, it needs protection from conflicting access. section 8.3

Protect shared data by synchronizing the code that uses it:

  • Use a mutex to guard operations such as reading or changing a bank balance; a thread that cannot acquire the lock waits until it is released. section 8.4
  • Make every thread use the same mutex for that resource. Inconsistent protection can leave the resource exposed to errors. section 8.4
  • Synchronization has a cost, so avoiding shared-resource access can sometimes avoid the need for it. section 8.7
# Chapter 8/race_condition/synced_bank_account.py
from threading import Lock
from unsynced_bank_account import UnsyncedBankAccount

class SyncedBankAccount(UnsyncedBankAccount):
    def __init__(self, balance: float = 0):
        super().__init__(balance)
        self.mutex = Lock()

    def deposit(self, amount: float) -> None:
        self.mutex.acquire()                            #1
        super().deposit(amount)
        self.mutex.release()                            #2

    def withdraw(self, amount: float) -> None:
        self.mutex.acquire()                            #1
        super().withdraw(amount)
        self.mutex.release()                            #2

See a Python bank-account example that acquires a lock around deposit and withdrawal operations. section 8.4

Chopsticks and dumplings

In the dining-philosophers problem, eating is the critical section: a philosopher locks both chopsticks before taking a dumpling, then releases them. section 9.1

How Grokking Concurrency covers it

How deep it goes
Defines the shared-resource code region and explains mutex protection, while leaving processor-level instructions largely abstract.
Examples
Uses a printer, bank-account transactions, and philosophers taking dumplings with chopstick locks.
What you can do after
Apply it via mutexes by acquiring a lock before shared-data operations and releasing it afterward.
How it connects
Connects synchronization and mutual exclusion with mutexes, race conditions, atomic operations, semaphores, and message passing.

Also explained in

Compare how 9 books explain critical region →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Concurrency. The section links point to the parts of the book it draws on.