Add a mutex to protect a variable shared between threads.