Applied concurrency examples
So far, we have learned about the main operations and behavior of goroutines and channels. These two concurrency mechanisms are important to understand, as they are pivotal to how Go implements concurrency. However, the Go standard library also includes concurrency primitives in its sync
package. It contains synchronization primitives with a broad variety of uses:
sync.Map
is a map implementation that is safe for concurrent use. We will explore how to create other thread-safe data structures in the next section.sync.Mutex
is an exclusion lock. It allows us to gatekeep resources for usage by one goroutine at a time. It is also possible to take a read-only or a read-write mutex depending on the problem being solved.sync.Once
is a specialized lock that can only be acquired once. This is useful for wrapping around statements, such as cleanup code, which should only run once.sync.Pool
is a temporary set of objects that are individually...