Golang Sync
Overview
The package Golang Sync is used for providing primitives for synchronization like Mutex, Waitgroup, Read and Write mutex as well as Pool and conditional variables.
Introduction to Golang Sync
The Golang Sync package is used for providing basic primitives of synchronization like mutual exclusion lock, and Waitgroup types most of which are used by low-level routines of the library. The synchronization of higher levels is done through a communication channel.
Mutex Golang
The Mutex, sync.Mutex, is used for providing a primitive of Mutex that allows the mutual exclusion of the resources that are shared in order to prevent the race condition. For instance, look at the example given below under the Mutex in golang.
Explanation:
In this example, the variable i is declared as 10, and the mutex is assigned with sync.Mutex and is locked. The variable i is incremented by a single goroutine and the mutex is unlocked.
Read and Write Mutex
The mutex sync.RWMutex is used for providing the reader as well as writer mutex that provides mutex which has methods similar to that of primitive mutex and allows concurrent read operation by using the methods Read and Write Mutex. In order to understand this better, look at the example given below:
Explanation:
In this example, the code allows a single goroutine to perform the read operation on the code. The sync.Mutex, however, allows only one reader and one writer at an instance of time to read the code.
Golang Waitgroups
The method sync.Waitgroup is used for providing the blocking mechanism for the goroutines and by using it, we can block the function execution until all the goroutines are done with the execution. The working of this method is that it creates a counter variable that stores the number of goroutines that are in the queue to wait.
When the goroutine is done with the execution, the counter variable is decreased by 1 and when the counter variable is back to 0, the Waitgroup unblocks the execution of the code. In order to add a value to the Waitgroup counter, the Add() method is used which takes an integer value as an argument. For removing the goroutine from the counter when the code is executed, the Done() method is used. To understand this better, look at the example given below:
Output:
Explanation:
In this example, the value of the Waitgroup counter is incremented by 1 by using the function Add() in the code. When the goroutine is completed, the counter is decremented by 1 by using the function Add() in the code.
Once Golang
The primitive sync.Once is used for ensuring that the function is executed only once. Look at the code given below to understand this better.
Output:
Explanation:
As you can see from this example, the code is executed only once because of the RunOnce function of golang and does not execute the for loop of the code.
Conclusion
- The package Golang sync is used for providing primitives for synchronization like Mutex, Waitgroup, Read and Write mutex as well as Pool and conditional variables.
- The synchronization of higher levels is done through a communication channel.
- The Mutex, sync.Mutex, is used for providing a primitive of Mutex that allows the mutual exclusion of the resources that are shared in order to prevent the race condition.
- The mutex sync.RWMutex is used for providing the reader as well as writer mutex that provides mutex which has methods similar to that of primitive mutex and allows concurrent read operation by using the methods Read and Write Mutex.
- The method sync.Waitgroup is used for providing the blocking mechanism for the goroutines and by using it, we can block the function execution until all the goroutines are done with the execution.
- The primitive sync.Once is used for ensuring that the function is executed only once.