March 2019
Beginner to intermediate
324 pages
7h 17m
English
Go also supports a read-write lock. A read-write lock differentiates between read and write operations. So, whenever you only perform concurrent read operations, the goroutines won't block. However, whenever you perform a write operation, all other reads and writes get blocked until the write lock is released. As always, this is best explained with an example, such as the following code:
var myRWMutex = &sync.RWMutex{}
A read-write lock in Go is represented by a pointer to a Go struct of the sync.RWMutex type, which is what we initialized in the preceding code snippet.
To perform a read operation, we make use of the RLock() and RUnlock() methods of the Go struct:
myRWMutex.RLock()fmt.Println(myMap[1])myRWMutex.RUnlock() ...
Read now
Unlock full access