Waitgroups and barriers are two synchronization abstractions that work on groups of executions (such as goroutines). We typically use waitgroups to wait for a group of tasks to complete. We use barriers to synchronize many executions at a common point.
We’ll start this chapter by examining Go’s bundled waitgroups using a couple of applications. Later, we’ll investigate two implementations of waitgroups: one built using semaphores and a more functionally complete one using condition variables.
Go does not bundle barriers in its libraries, so we’ll build our own barrier type. Then we’ll employ this barrier type in a simple concurrent matrix multiplication algorithm.
With waitgroups, we can have a goroutine wait for a set of concurrent tasks to complete. We can think of a waitgroup as a project manager managing a set of tasks given to different workers. Once the tasks are all complete, the project manager notifies us.