Make sure all threads are dead before calling fork()

fixes #7653