> For the complete documentation index, see [llms.txt](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/goroutine/mutex-mutual-exclusion.md).

# Mutex (Mutual Exclusion)

Mutex -> digunakan untuk mengatasi race condition -> locking dan unlocking

Dimana ketika kita melakukan locking terhadap mutex, maka tidak ada yang bisa melakukan locking lagi sampai kita melakukan unlock.&#x20;

Jika ada beberapa goroutine melakukan lock terhadap Mutex, maka hanya 1 goroutine yang dieksekusi, setelah goroutine tersebut melakukan unlock, baru goroutine selanjutnya melakukan lock lagi.

Kekurangan -> ada kemungkinan beberapa go routine saling menggu untuk melakukan lock -> bisa menimbulkan deadlock.

```go
package main
 
import (
    "fmt"
    "sync"
)

var wg sync.WaitGroup
var m sync.Mutex

func f(v *int, wg *sync.WaitGroup) {
    m.Lock()
    *v++
    m.Unlock()
    wg.Done()
}
 
func main() {
    var v int = 0
 
    for i := 0; i < 1000; i++ {
        wg.Add(1)
        go f(&v, &wg)
    }
 
    wg.Wait()
    fmt.Println("Finished", v)
}
```

```
Finished 1000
```

```go
package main
 
import (
    "fmt"
    "sync"
)

var wg sync.WaitGroup
var m sync.Mutex

func f(v *int, wg *sync.WaitGroup) {
    m.Lock()
    *v++
    m.Unlock()
    wg.Done()
}
 
func main() {
    var v int = 0
 
    for i := 0; i < 10000; i++ {
        wg.Add(1)
        go f(&v, &wg)
    }
 
    wg.Wait()
    fmt.Println("Finished", v)
}
```

```
Finished 10000
```

```go
package main
 
import (
    "fmt"
    "sync"
)

var wg sync.WaitGroup
var m sync.Mutex
 
func main() {
    var v int = 0
 
    for i := 0; i < 10000; i++ {
        wg.Add(1)
        go func() {
            m.Lock()
            v++
            m.Unlock()
            wg.Done()
        }()
    }
    
    wg.Wait()
    fmt.Println("Finished", v)
}
```

```
Finished 10000
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rafli-ramadhan.gitbook.io/golang-example-code-by-topic/goroutine/mutex-mutual-exclusion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
