Grokking Concurrency in Go
0% completed
RWMutex for Read Heavy Data
Eight goroutines each read a shared table five times, and each read holds a lock for 2 ms. That is 40 reads and 80 ms of time spent inside the lock. With a sync.Mutex, only one goroutine can read at a time. The 40 reads run one after another, and the whole job takes at least 80 ms.
None of those 40 reads changes the table, so there is no reason to keep them apart. If all eight goroutines could read at the same time, the job would take about 5 reads of 2 ms each, which is 10 ms. The program below measures both versions.
One possible output (measured on one machine):
Mutex: 90ms
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%