Fix some bugs that may occur in multi-thread programs.