Junior — Middle
Is mutex a thread synchronization tool?
sobes.tech AI
Answer from AI
Yes, mutex (mutual exclusion) is a synchronization tool for threads. In Go, it is represented by the sync.Mutex structure, which allows limiting concurrent access to shared resources, preventing race conditions. When one thread (goroutine) acquires the mutex, others attempting to acquire it are blocked until it is released.
Example of using mutex in Go:
var mu sync.Mutex
var counter int
func increment() {
mu.Lock() // acquire mutex
defer mu.Unlock() // release mutex
counter++
}