What is a mutex and how does it work? Imagine a big office (your program) with many assets (shared resources) and many employees (threads). As an example, all employees share one common resource - a toilet. All employees have agreed to use a label on the toilet's door (a mutex). When employee wants to use a toilet he checks a label on a door (locks a mutex): if it is "Occupied" he waits (blocks on a mutex), once it is "Free" he enters the toilet and changes the label to "Occupied" (mutex lock succeeded). When employee is done they exit he goes out and changes the label to "Free" (unlocks mutex). If there are many people in the office, there may be a queue (many threads blocked on a mutex). Then it is up to a manager (threads implementation algorithm) to decide who may enter next. Usually it is the first person in the queue (FIFO algorithm). For this system these rules must be followed: 1. Everybody who wants to pee MUST use a label, enter only when it is "Free" and set the label to "Occupied" when entering (lock mutex). Otherwise confusion guaranteed. 2. When done the label must be set to "Free" (unlock mutex). As you can see these rules are just an agreement amongst the office staff (program logic). The label on the toilet's door (mutex) and the toilet itself (resource) are totally independent. The label can be anywhere, for example, at the help desk. The important point is that everybody knows where it is and how it should be used. Suppose somebody in the office does NOT follow the agreement (some thread does not use mutex when accessing a resource). That person enters the toilet paying no attention to the label. If someone is already sitting there then, as you can imagine, BAD things may happen. Authors: Denis Kozadaev, Sergey Lyubka. June 2007.