What is mutex?
Mutex is stand for mutual exclusion object. In computer programming, a mutex is a program object that allows multiple program threads (light weight process) to share the same resource, such as file access, but not simultaneously. When a program is started, a mutex is created with a unique name. After this stage, any thread that needs the resource must lock the mutex from other threads while it is using the resource. If the mutex is already locked, a thread needing the resource is typically queued by the system and then given control when the mutex becomes unlocked (when once more, the mutex is locked during the new thread’s use of the resource).The mutex is set to unlock when the data is no longer needed or the routine is finished.
Use of Mutex
A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work. As long as the buffer is filled by producer, the consumer needs to wait, and vice versa. At any point of time, only one thread can work with the entire buffer.