But because the two tasks share resources, threads must be ready to synchronize access to shared resources. The two synchronization primitives are locks and condition variables. Locks Locks provide mutual exclusion. Most lock interfaces provide an initialization function, a lock function, and an unlock function. This will allow threads waiting for the lock to get the lock.
Contention is the state where multiple threads are attempting to acquire a lock at the same time.
When there is contention for acquiring a lock, the time it takes to acquire the lock can be orders of magnitude greater than the time it takes when there is no contention. Make locked sections of code short. Large areas of code encapsulated with locks probably indicates a poor code design. Condition variables Condition variables are synchronization primitives that allow you to specify an arbitrary condition on which to block. To do this, thread A would first acquire a lock.