use mutex to solve race conditions