How it works...
This recipe is an example of comparing a critical path of code. For example, sometimes your application must execute certain functionality often, maybe every call. In this case, we've written an atomic counter that can add or read values from multiple go routines.
The first solution uses RWMutex and Lock or RLock objects to write and read, respectively. The second uses the atomic package that provides the same functionality out of the box. We make the signatures of our functions the same, so benchmarks can be reused with minor modifications and so that either can satisfy the same atomic integer interface.
Lastly, we write standard benchmarks for adding values and reading them. Then, we write a parallel benchmark that calls ...
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