July 2019
Intermediate to advanced
458 pages
12h 12m
English
When designing an application, this kind of mutex is not always the obvious choice, because in a scenario where there is a greater number of read locks and a few write ones, the mutex will be accepting incoming more read locks after the first, letting the write operation wait for a moment where there are no read locks active. This is a phenomenon referred to as write starvation.
To check this out, we can define a type that has both a write and a read operation, which take some time, as shown in the following code:
type counter struct { m sync.RWMutex value int}func (c *counter) Write(i int) { c.m.Lock() time.Sleep(time.Millisecond * 100) c.value = i c.m.Unlock()}func (c *counter) Value() int { c.m.RLock() time.Sleep(time.Millisecond ...Read now
Unlock full access