Essential Go Mutex  Suggest an edit

Mutex gotchas

Don’t copy mutexes

A copy of sync.Mutex variable starts with the same state as original mutex but it is not the same mutex.

It’s almost always a mistake to copy a sync.Mutex e.g. by passing it to another function or embedding it in a struct and making a copy of that struct.

If you want to share a mutex variable, pass it as a pointer *sync.Mutex.

Mutex is not recursive

In some languages mutexes are recursive i.e. the same thread can Lock the same mutex multiple times.

In Go sync.Mutex is non-recursive. If the same goroutine calls Lock twice on the same mutex, the program will deadlock.

  ↑ ↓ to navigate     ↵ to select     Esc to close