February 2018
Intermediate to advanced
340 pages
9h 43m
English
The synchronization primitive Mutex is provided by the package sync. The Mutex works as a lock above the secured section or resource. Once the goroutine calls Lock on the Mutex and the Mutex is in the unlocked state, the Mutex becomes locked and the goroutine gets exclusive access to the critical section. In case the Mutex is in the locked state, the goroutine calls the Lock method. This goroutine is blocked and needs to wait until the Mutex gets unlocked again.
Note that in the example, we use the Mutex to synchronize access on a slice primitive, which is considered to be unsafe for the concurrent use.
The important fact is that the Mutex cannot be copied after its first use.
Read now
Unlock full access