Updated April 12, 2023
Introduction to Golang Mutex
The following article provides an outline for Golang Mutex. This allow us to write code in such a way that code will be blocked till resources will not be released which helps to avoid race condition (Basically race condition means you have made a call for code block to perform its job and mean before completing the processing we are again calling which will lead to race condition). So basically there are two important things which will happen one is locking (lock the block of code till complete), unlocking (once the work of the code do unlock the code block), for example if you have any function and you want function to get any other call only after the completion task.
Syntax of Golang Mutex
In the below syntax we can see the locking and unlocking of the operation.
- First we lock with the help of the mutex, remember to use the mutex we need to import the package called sync, and on sync we can call the mutex locking.
- Here locking means it will not take any other call till it will not complete the current call.
- Second we are performing the operation, you can take any operation like addition or calculation of the amount from your current account also.
- Once the activity for which the code block is there will be done the unlock will be called.
- Unlock will remove the locking which we applied on the code block and again another call be taken.
- Remember we use this only for locking to any very critical type of the code block and that code block is not needed to run in the concurrent (Here concurrent means the call should be made only for the code which can not run for multiple time for an user or related one specific attribute ).
Given below is the syntax:
mutex.Lock()
y = y - number
mutex.Unlock()
How does Mutex work in Go Language?
You must have withdraw money from ATM many times suppose in your account there is 5000 rupees and you are doing ATM transaction as well as you are paying to some other shop also by using online medium of transaction in that case it can be possible that in ATM you have withdraw amount 5000 and 5000 you are trying to pay to your shop by online, now how to control this because if we will not control this than both the transaction can be done if both happens at the same time. Hence it will be a loss for the banks, so to avoid this kind of situations we use the concept of mutex which allow us for locking of particular code block. Here such case it will lock the code block for once it will find any transaction and will not allow other transaction till previous transaction which started will not complete.
Given below is the working of Mutex:
- Mutex is the part of the sync, so to use the mutex we need to import the sync package of the go language.
- Once we use the mutex.lock() whatever the code lock next to it will be there it will block that code till the execution of the code block will not complete.
- Code blocks can be anything like calculation of any arithmetic calculation or calculation of the payment transactions.
- Finally the process of unlocking mutex.unlock so once the unlocking get call means the code has completed and next execution for the code block can be allowed.
Examples of Golang Mutex
Given below are the examples mentioned:
Here we will see the working of the mutex. If we want to test the example then we can make a file with any name, we are taking name mutex.go and copy paste the codes of example on the file and run the command go run mutex.go and we can get the output for the execution.
Example #1
In this example we can see the race conditions which means code block is not getting locked and it will be called simultaneously and hence before completion of the previous call it will make another call and hence it will go into the race conditions.
Code:
package main
import (
"fmt"
"sync"
)
var UPT = 0
func worker(st *sync.WaitGroup) {
UPT = UPT + 1
st.Done()
}
func main() {
var s sync.WaitGroup
for i := 0; i < 1000; i++ {
s.Add(1)
go worker(&s)
}
s.Wait()
fmt.Println("The y value is", UPT)
}
Output:
Example #2
In the below example we are performing the addition and we have used loop also. Our goal for this example is to execute only after the completion of the one call or in more technical way we are trying to lock the operation UTV+1 and each time the next call be made only after the previous call be completed. With the help of this example we can see that we are able to lock the certain block of code and hence we are avoiding the unnecessary race conditions. We have use the unlock which means each time when the code block complete it will be unlock for again and ready for uses.
Code:
package main
import (
"fmt"
"sync"
)
var UTV = 0
func worker(wt *sync.WaitGroup, k *sync.Mutex) {
k.Lock()
UTV = UTV + 1
k.Unlock()
wt.Done()
}
func main() {
var s sync.WaitGroup
var n sync.Mutex
for i := 0; i < 1001; i++ {
s.Add(1)
go worker(&s, &n)
}
s.Wait()
fmt.Println("The y value is", UTV)
}
Output:
Conclusion
From this article we saw the basic concept of the mutex in the go language, we saw the working of the mutex and we also saw about the syntax of the mutex. We focused on the some of the important examples of the mutex which can be used for real world.
Recommended Articles
This is a guide to Golang Mutex. Here we discuss the introduction, syntax, and working of mutex in go language along with programming examples. You may also have a look at the following articles to learn more –