...

/

Handling Concurrency and Parallelism with Go

Handling Concurrency and Parallelism with Go

This lesson discusses the problem of synchronization in parallelism and concurrency. It explains how Go introduces goroutines to solve this problem.

Introduction

As expected of a 21st century programming language, Go comes with built-in support for communication between applications and support for concurrent applications. These are programs that execute different pieces of code simultaneously, possibly on different processors or computers. The basic building blocks for structuring concurrent programs are goroutines and channels. Their implementation requires support from the language, the compiler, and the runtime. The garbage collection which Go provides is also essential for easy concurrent programming. This support must be baked into the language with specific types (chan), keywords (go, select) and constructs (goroutines), and not just added as a separate library (like java.util.concurrent for Java).

Do not communicate by sharing memory. Instead, share memory by communicating. Communication forces coordination.

What are goroutines?

An application is a process running on a machine; a process is an independently executing entity that runs in its own address space in memory. A process is composed of one or more operating system threads that are simultaneously executing entities that share the same address space. Almost all real programs are multithreaded, so as not to introduce wait times for the user or the computer, or to be able to service many requests simultaneously (like web servers), or to increase performance and throughput (e.g., by executing code in parallel on different datasets). Such a concurrent application can execute on one processor or core using several threads. Still, it is only when the same application process executes at the same point in time ...