6 Synchronizing with waitgroups and barriers

 

This chapter covers

  • Waiting for completed tasks with waitgroups
  • Building waitgroups with semaphores
  • Implementing waitgroups using condition variables
  • Synchronizing concurrent work using barriers

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.

6.1 Waitgroups in Go

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.

6.1.1 Waiting for tasks to complete with waitgroups

 
 

6.1.2 Creating a waitgroup type using semaphores

 
 
 
 

6.1.3 Changing the size of our waitgroup while waiting

 
 

6.1.4 Building a more flexible waitgroup

 
 

6.2 Barriers

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest