DEV Community

Sukma Rizki
Sukma Rizki

Posted on

What is Mutex ?

Before using mutex? It's a good idea for you to learn about race conditions, because these two concepts are closely related to each other.
A race condition is a condition where more than one goroutine accesses data together at the same time. When this happens, the data value becomes chaotic. In concurency programming this value often occurs.

Mutex changes the access level of data to executive, making the data only able to be consumed (read/written) by one goroutine. When a race condition occurs, the goroutine is lucky enough to be able to access the data. Other goroutines (whose running time happens to be the same) will be forced to wait, until the wait for using the data is complete.

Go provides sync.Mutex which can be used to lock and unclock data

Top comments (2)

Collapse
 
hermessantos profile image
Hermes Santos • Edited

I don't know Go, but reading this I assume that Go creates threads during the code execution, right? Doesn't that make it slower?

Collapse
 
sukmarizki04 profile image
Sukma Rizki

But do the design in such a way that you know Go will run quickly