mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-17 18:51:17 +00:00
first
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"question": "Can readers and writers use RWMutexes at the same time?",
|
||||
"answers": [
|
||||
"No",
|
||||
"Yes"
|
||||
]
|
||||
}
|
||||
51
course/14-mutexes/exercises/6b-rw_mutex_review/readme.md
Normal file
51
course/14-mutexes/exercises/6b-rw_mutex_review/readme.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Read/Write Mutex Review
|
||||
|
||||
Maps are safe for concurrent *read* access, just not concurrent read/write or write/write access. A read/write mutex allows all the readers to access the map at the same time, but a writer will still lock out all other readers and writers.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := map[int]int{}
|
||||
|
||||
mux := &sync.RWMutex{}
|
||||
|
||||
go writeLoop(m, mux)
|
||||
go readLoop(m, mux)
|
||||
go readLoop(m, mux)
|
||||
go readLoop(m, mux)
|
||||
go readLoop(m, mux)
|
||||
|
||||
// stop program from exiting, must be killed
|
||||
block := make(chan struct{})
|
||||
<-block
|
||||
}
|
||||
|
||||
func writeLoop(m map[int]int, mux *sync.RWMutex) {
|
||||
for {
|
||||
for i := 0; i < 100; i++ {
|
||||
mux.Lock()
|
||||
m[i] = i
|
||||
mux.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readLoop(m map[int]int, mux *sync.RWMutex) {
|
||||
for {
|
||||
mux.RLock()
|
||||
for k, v := range m {
|
||||
fmt.Println(k, "-", v)
|
||||
}
|
||||
mux.RUnlock()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
By using a `sync.RWMutex`, our program becomes *more efficient*. We can have as many `readLoop()` threads as we want, while still ensuring that the writers have exclusive access.
|
||||
|
||||
Reference in New Issue
Block a user