There are several forms of synchronization in software; one of the commonly encountered ones, and indeed one that we shall be working with quite a bit, is called locking. A lock, in programming terms, and as seen by the application developer, is ultimately a data structure instantiated as a variable.
When one requires a critical section, just encapsulate the code of the critical section between a lock and a corresponding unlock operation. (For now, don't worry about the code-level API details; we shall cover that later. Here, we are just focusing on getting the concepts right.)
Let's represent the critical section, along with the synchronization mechanism—a lock— using a diagram (a superset of the preceding Figure 3):
The basic premise of a lock is as follows:
- Only one thread can hold or own a lock...