It seems that you forgot to Resume your third thread.
I'd like to say that there is several potential flaws in your code. I'd recommend you to Lock each place where you work with your shared variables ( yes, even if you only read their value ). This time it may work, but sometimes even reading object which is being modified by other thread can be dangerous.
Also you can apply a bit more complex Free/Lock pattern to your code, so you don't need to call acquire/release manually
class AutoLock
{
public:
AutoLock(Lock& l)
:lock(l)
{
lock.acquire();
}
~AutoLock()
{
lock.release();
}
Lock& lock;
};
Then you can rewrite your functions to make them cleaner and safer:
static unsigned __stdcall sub(void *args)
{
while(alive)
{
cout << "tq";
{
examples::AutoLock lock(lock_1);
test_q.push(1);
}
::Sleep(500);
}
return 0;
}
Note that separate scope is required because otherwise critical section lock_1 will be locked until ::Sleep(500) executed which is not what you normally want.