Fixed race condition in Thread creation