Part 2. Message passing

 

In the first part of the book, we talked about how to use memory sharing to enable communication between threads of execution. In this second part, we’ll explore message passing, which is a different way for executions to communicate. In message passing, threads of executions pass copies of messages to each other whenever they need to communicate. Since these executions are not sharing memory, we eliminate the risks of many types of race conditions.

Go takes inspiration from a concurrency model called communicating sequential processes (CSP), which is a formal language for describing interactions of concurrent programs. In this model, processes connect to each other by communicating via synchronous message passing. In the same fashion, Go provides us with the concept of the channel, which enables goroutines to connect, synchronize, and share messages with one another.

In this part of the book, we’ll explore message passing and the various tools and programming patterns we can use to manage this form of communication.