Lock, mutex, and semaphore
Lock and mutex are locking constructs that allow only one thread to access a protected resource. Lock is a shortcut implementation that uses another higher-level synchronization class called Monitor.
Semaphore is a locking construct that allows a specified number of threads to access a protected resource. Lock can only synchronize access inside a process, but if we need to access a system-level resource or shared memory, we need to actually synchronize access across multiple processes. A mutex allows us to synchronize access to resources across processes by providing a kernel-level lock.
The following table provides a comparison of the capabilities of these constructs:
As we can see, Lock and Mutex only allow single-thread ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access