Golang Mutex

How to synchronize codes to allow mutual executions

Israel Josué Parra Rosales
3 min readAug 1, 2022

Maybe you are familiarized with programing languages that handles with memory access synchronization, if yes probably you recognize the term Mutex, if not don’t worry because Mutex is very easy to understand and I will show you an example to make it clear.

Mutex stands for “mutual execution” and helps us to guard critical sections of your program, especially the parts that share memory access between different process.

By implementing Mutex we ar adding a concurrent-safe logic to our shared codes but is important to know that is responsibility to developers to synchronize the accessto the memory and that is possible using mutex.

In a few words, a mutex will help us to coordinate the access (set/get) to a shared memory that keeps some value. Allowing the access to just one process at the time, for example, one for write the value and other to get values.

Let us see an example:

Imaging a restaurant where the chefs are preparing the food orders and the waiters are taking it to the customers.

In this example we can see in action a producer and consumer example, in the code we will simulate the chefs with a goroutines, the waiters in another goroutines and the shared memory is an array with the prepared food.

Let me give you a brief code explanation.

1. In the line 9 we define a simple int variable that will be shared between our two process.

2. In the line 11 we could see defined a function “chefPrepareOrder” whos takes two params one for the mutex to lock and unlock the function logic and other one for waitgroup.

As you can see there we are locking at the start of the function and unlocking when the execution is funished.

3. In the line 23 we can find “waiterTakeOrder” function whos takes two params one for the mutex to lock and unlock the function logic and other one for waitgroup.

As you can see there we are locking at the start of the function and unlocking when the execution is finished.

4. The functions “chefPrepareOrder” and “waiterTakeOrder” will share the value of “ordersCount” the first one add 1 to the value and the second one rest 1 to the value.

5.- Finally the main, here we define the mutex variable that will be used to synchronize the codes and the waitgroup. Also we call the “chefPrepareOrder” and “waiterTakeOrder” functions is a separate goroutines that is why we need the “waitGroup”.

Let us code:

Output:

Easy right?, now you can start to implement and look for study cases where you could synchronize your code executions.

The next step is learn about channels, it will help and provide more power to our goroutines.

Next Step: How to implement channels

This article is related to the Golang concurrency tutorial that you can find in the following link:

https://medium.com/@josueparra2892/concurrency-in-go-bf93e23bebd4

--

--

Israel Josué Parra Rosales

I'm a software developer with more than 11 years of experience. The last 9 years I have been working with Go. I love to learn and share my knowledge.