Remove mixing declarations and code
Move cond_wait to end of loop

First check if there's anything pushed to the thread queue. Proper way
to do that.