We study the following locks:
package main import ( "fmt" "sync" "time" ) var wg sync.WaitGroup var mutex sync.RWMutex func write() { defer wg.Done() mutex.Lock() fmt.Println("Writing ...") time.Sleep(time.Second * 1) mutex.Unlock() } func read() { defer wg.Done() mutex.RLock() fmt.Println("Reading ...") time.Sleep(time.Second * 1) mutex.RUnlock() } func main() { for r := 0; r < 10; r++ { wg.Add(1) go write() } for r := 0; r < 10; r++ { wg.Add(1) go read() } wg.Wait() }
- In
RWMutex
, when we are writing we useLock()
to block any read and write access until weUnlock()
it. - However, when we use
RLock()
, we are telling compiler we allow concurrent reads. - If we execute the code above, we have:
and 1s later:Writing ...
and then the following 1s later one another:Reading ... Reading ... Reading ... Reading ... Reading ... Reading ... Reading ... Reading ... Reading ... Reading ...
Writing ... // 1s Writing ... // 1s Writing ... // 1s Writing ... Writing ... . Writing ... . Writing ... . Writing ... Writing ... // 1s