3 Thread communication using memory sharing

 

This chapter covers

  • Using inter-thread communication with our hardware architecture
  • Communicating with memory sharing
  • Recognizing race conditions

Threads of execution working together to solve a common problem require some form of communication. This is what is known as inter-thread communication (ITC), or inter-process communication (IPC) when referring to processes. This type of communication falls under two main classes: memory sharing and message passing. In this chapter, we will focus on the former.

Memory sharing is similar to having all our executions share a large, empty canvas (the process’s memory) on which each execution gets to write the results of its own computation. We can coordinate the executions in such a way that they collaborate using this empty canvas. In contrast, message passing is exactly what it sounds like. Just like people, threads can communicate by sending messages to each other. In chapter 8, we’ll investigate message passing in Go using channels.

The type of thread communication we use in our applications will depend on the type of problem we’re trying to solve. Memory sharing is a common approach to ITC, but as we shall see in this chapter, it comes with a certain set of challenges.

3.1 Sharing memory

3.2 Memory sharing in practice

3.2.1 Sharing a variable between goroutines

3.2.2 Escape analysis

Summary