Threads also share open files. Threads make it easier for you to share data between the two tasks. 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.