# RW Mutex vs Mutex

RW Mutex -> Digunakan untuk supaya beberapa function dapat membaca data yang sama secara bersamaan.

## Mutex Only

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

var (
	wg sync.WaitGroup
	m sync.Mutex
	rw sync.RWMutex
	v int = 0
)
 
func main() {
    for i := 1; i <= 10; i++ {
		fmt.Printf("\nITERASI-%d\n", i)
		wg.Add(2)

		go func() {
			m.Lock()
			fmt.Println("Write lock")
			v++
			fmt.Println("Write unlock")
			m.Unlock()
			wg.Done()
		}()

		go func() {
			m.Lock()
			fmt.Println("Read lock")
			fmt.Println(v)
			fmt.Println("Read unlock")
			m.Unlock()
			wg.Done()
		}()
    }
    
    wg.Wait()
    fmt.Println("\nFinished", v)
}
```

```
ITERASI-1

ITERASI-2

ITERASI-3

ITERASI-4

ITERASI-5

ITERASI-6
Write lock
Write unlock
Read lock
1
Read unlock
Write lock
Write unlock
Read lock
2
Read unlock
Write lock
Write unlock
Read lock
3
Read unlock
Write lock
Write unlock
Read lock
4
Read unlock
Write lock
Write unlock
Read lock
5
Read unlock
Read lock
5
Read unlock
Write lock
Write unlock

ITERASI-7

ITERASI-8

ITERASI-9

ITERASI-10
Write lock
Write unlock
Read lock
7
Read unlock
Read lock
7
Read unlock
Write lock
Write unlock
Write lock
Write unlock
Read lock
9
Read unlock
Read lock
9
Read unlock
Write lock
Write unlock

Finished 10
```

## RW Mutex

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

var (
	wg sync.WaitGroup
	m sync.Mutex
	rw sync.RWMutex
	v int = 0
)
 
func main() {
    for i := 1; i <= 10; i++ {
		fmt.Printf("\nITERASI-%d\n", i)
		wg.Add(2)

		go func() {
			m.Lock()
			fmt.Println("Write lock")
			v++
			fmt.Println("Write unlock")
			m.Unlock()
			wg.Done()
		}()

		go func() {
			rw.RLock()
			fmt.Println("Read lock")
			fmt.Println(v)
			fmt.Println("Read unlock")
			rw.RUnlock()
			wg.Done()
		}()
    }
    
    wg.Wait()
    fmt.Println("\nFinished", v)
}
```

```
ITERASI-1

ITERASI-2
Write lock
Write unlock
Write lock
Write unlock
Read lock

ITERASI-3
Read lock
2
Read unlock
Read lock
Write lock
Write unlock

ITERASI-4

ITERASI-5
2
Read unlock
Write lock
Write unlock
Write lock
Write unlock
2
Read unlock

ITERASI-6

ITERASI-7

ITERASI-8
Read lock
5
Read unlock
Read lock
Read lock
5
Read unlock
Write lock
Write unlock
Write lock
Write unlock
Write lock
Write unlock
5
Read unlock
Read lock
8
Read unlock
Read lock
8
Read unlock

ITERASI-9

ITERASI-10
Write lock
Read lock
9
Read unlock
Read lock
9
Read unlock
Write unlock
Write lock
Write unlock

Finished 10
```


---

# Agent Instructions: 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/rw-mutex-vs-mutex.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.
