Book Image

Effective Concurrency in Go

By : Burak Serdar
5 (1)
Book Image

Effective Concurrency in Go

5 (1)
By: Burak Serdar

Overview of this book

The Go language has been gaining momentum due to its treatment of concurrency as a core language feature, making concurrent programming more accessible than ever. However, concurrency is still an inherently difficult skill to master, since it requires the development of the right mindset to decompose problems into concurrent components correctly. This book will guide you in deepening your understanding of concurrency and show you how to make the most of its advantages. You’ll start by learning what guarantees are offered by the language when running concurrent programs. Through multiple examples, you will see how to use this information to develop concurrent algorithms that run without data races and complete successfully. You’ll also find out all you need to know about multiple common concurrency patterns, such as worker pools, asynchronous pipelines, fan-in/fan-out, scheduling periodic or future tasks, and error and panic handling in goroutines. The central theme of this book is to give you, the developer, an understanding of why concurrent programs behave the way they do, and how they can be used to build correct programs that work the same way in all platforms. By the time you finish the final chapter, you’ll be able to develop, analyze, and troubleshoot concurrent algorithms written in Go.
Table of Contents (13 chapters)

The producer-consumer problem

In the previous chapter, we implemented a version of the producer-consumer problem using condition variables and mentioned that most of the time, the condition variables can be replaced by channels. The producer-consumer implementations we will work on in this chapter demonstrate this point. Some concurrency problems, such as the producer-consumer problem, are by their nature message-passing problems, and trying to solve them using shared-memory utilities results in unnecessarily complicated and lengthy code.

At the core of the producer-consumer problem is limited intermediate storage. At a high level, the producer-consumer problem contains processes that produce objects at various rates, and consumers that consume those objects at various rates, with limited storage in between the two that is used to store the produced objects until they are consumed. The producer-consumer problem is relevant in any system where a balance between the production of...