Fix races by making synchronous communications between the threads