Threads share global variables, which can cause problems if two threads attempt to modify the same variable at the same time. Mutexes are used to synchronize access to shared variables between threads. A mutex has two operations - lock and unlock. When a thread locks a mutex, it gains exclusive access to the critical section of code that accesses shared variables. Other threads attempting to lock the mutex will block until it is unlocked. This ensures only one thread at a time can access the shared variables.