Locking a resource helps in non-ambiguous results, but locking can also lead to a deadlock. A deadlock is a situation wherein a thread has acquired the lock for one resource and wants to acquire the lock for a second resource. However, at the same time, another thread has acquired the lock for the second resource, but wants the lock for the first resource. Because the first thread will keep waiting for the second resource lock to be free and the second thread will keep waiting for the first resource lock to be free, the threads will not be able to proceed further, and the application will hang (as the following diagram illustrates):
In this recipe, we will use a stack. A stack requires two operations—push and pop. To make only one thread execute a push or pop operation at a time, we will use two mutex objects—pop_mutex...