Without this lock it's possible that thread_wait_pop will stuck at the point where it await for new task in the queue but in fact exit was requested already. This ended up in deadlock in some circumstances. Really random because it totally depends on timings.