Critical Sections

  • A region of code

  • Must only be executed by one thread at a time

  • Usually when accessing a shared resource

    • Shared data, network connection, hardware device
  • The thread "enters" the critical section

    • It starts executing the code in the critical section
  • The thread "leaves" the critical section

    • It has executed all the code in the critical section

Single track Railway

  • The track can only be used by one train at a time
  • When a train approaches this section of track:
    • If there is another train in the section, wait
    • Proceed only when there is no other train

Locking Protocol

  • One thread can enter the critical section

    • All the other threads are "locked out"
    • Only this thread can execute the code in the critical section
  • The thread leaves the critical section

    • One of the other threads can now enter it