Sobes.tech
Junior — Middle

What is the role of a mutex in ensuring safety in multithreaded programming?

sobes.tech AI

Answer from AI

A mutex (mutual exclusion) is used to synchronize access to shared resources in multithreaded programs. Its role is to ensure that only one thread can execute a critical section of code at a time, preventing race conditions and data corruption.

Example: if several threads try to modify a shared counter simultaneously, the mutex guarantees that the changes are sequential and correct, blocking access to other threads until the operation is complete.