chapter nine

9 Concurrency

 

This chapter covers

  • Go’s concurrency model
  • Leveraging internal libraries to manage concurrent tasks
  • Creating channels for asynchronous communication
  • Applying concurrent patterns

Imagine you have a list of chores: bake a chicken, do the laundry, and send an email. You might work on all three at once. While the chicken bakes and the laundry runs, you can send your email. In this scenario, multiple tasks are happening concurrently. When two or more tasks are literally being executed at the same time, such as the oven and washing machine running simultaneously, that’s called parallelism.

It’s common to confuse concurrency and parallelism. Concurrency is about managing multiple tasks at once, allowing progress on several activities by switching between them. Parallelism is about actually performing multiple tasks at the same time, typically on separate processors or cores.

Figure 9.1 Concurrency vs Parallelism
CH09 F01 Holmes

Modern applications rely on concurrency to remain responsive, such as letting you receive a call while browsing the web or sending an email. Without concurrency, programs would be forced to finish one task before starting another, making them slow and unresponsive.

9.1 When to use concurrency

9.2 Sync package

9.2.1 Wait Groups

9.2.2 Error Groups

9.2.3 Mutexes

9.3 Adding some Context

9.4 Channels

9.5 Concurrency Patterns

9.5.1 Workers

9.5.2 Fan out and Fan In

9.6 Helpful Concurrency Tips

9.6.1 Best Practices for Go Concurrency

9.6.2 When to Use Concurrency in Go

9.6.3 When Not to Use Concurrency in Go

9.7 Summary