Remove sleep used for race condition

Move exit condition of threads at the end of their while loop
This is still a bit clumsy ...