treminate flag, join all threads, check empty, one more flag mutex