There are 2 questions here:
First, when you create a lock object
unique_lock<mutex> lock(cars_mux);
then cars_mux blocked. Thus, it is an error ( undefined behavior ) to try to block cars_mux again in the same thread that you are trying to do right away inside the while loop on
cars_mux.lock();
Secondly, there is no way to attach threads, since there is no way to exit the while(true) in the task - the cars will continue to park forever! You don't need a while .
If you remove the first cars_mux.lock(); , the corresponding attempt to unlock at the end of the while and while , you should get the desired behavior.
source share