Locks can be used to limit access to a protected resource to only a single thread or group of threads. To be able to implement locking efficiently, we need to identify appropriate critical sections that can be protected via locking primitives.
Introduction to locking primitives
How locking works
When we apply a lock to a shared resource, the following steps are performed:
- A thread or group of threads access a shared resource by acquiring a lock.
- Other threads that cannot get access to a lock go into a wait state.
- As soon as the lock is freed by one of the threads, it is acquired by another thread, which starts its execution.
To understand locking primitives, we need to understand various thread states, as well as concepts...