How it works...
For our map recipe, we used a ReadWrite mutex. The idea behind this mutex is that any number of readers can acquire a read lock, but only one writer can acquire a write lock. Additionally, a writer cannot acquire a lock when anyone else (reader or writer) has one. This is useful because reads are very fast and non-blocking when compared to a standard mutex. Whenever we want to set data, we use Lock() object and whenever we want to read data we use RLock(). It is critical that you use Unlock() or RUnlock() eventually so that you don't deadlock your application. A defer Unlock() object can be useful, but may be slower than calling Unlock() manually. This pattern may not be flexible enough when you want to group additional actions ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access