The sync.RWMutex type

The sync.RWMutex type is another kind of mutex – actually, it is an improved version of sync.Mutex, which is defined in the rwmutex.go file of the sync directory as follows:

type RWMutex struct { 
        w           Mutex  // held if there are pending writers 
        writerSem   uint32 // semaphore for writers to wait for completing readers 
        readerSem   uint32 // semaphore for readers to wait for completing writers 
        readerCount int32  // number of pending readers 
        readerWait  int32  // number of departing readers 
} 

In other words, sync.RWMutex is based on sync.Mutex with the necessary additions and improvements.

Now, let us talk about how sync.RWMutex improves sync.Mutex. Although only one function is allowed to perform write operations using a sync.RWMutex ...

Get Mastering Go - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.