Concurrency vs Parallelism: A Visual Explanation

Concurrency vs Parallelism: A Visual Explanation

This article was first published on my blog here

Concurrency and parallelism are often used interchangeably, but as we’ll explore in this article, they represent two different concepts. While both involve managing multiple tasks, they differ in how operations are executed and coordinated.

Before we begin, let’s describe the most straightforward way of performing operations—sequentially. Sequential processing is step-by-step, one-at-a-time, finish-this-before-starting-that kind of processing.

To illustrate, let’s consider a group of five friends (Alex, Bayo, Caleb, Dipo, and Eve) who are hanging out and planning to make potato fries. They have a total of 10 raw potatoes and decide to divide the work of peeling them equally, with each person peeling two. Eve suggests that they first peel all the potatoes before moving on to cutting them into shape. Smart woman.

The problem? There is only one knife, how should they proceed?

One approach involves one person taking the knife, peeling the potatoes, and then handing the knife to the next person when they are done.

This step-by-step, one-person-finishes-before-another is an example of Sequential Processing. The idea is that we have more than one process that needs to execute but a process can only start when another has finished execution.

Vertically, there are no two active processes at the same time and each process starts only if the previous one has run to completion

Concurrency

Let’s start again with each person still having two unpeeled potatoes to work with and only one knife available. Now, imagine that one of the five friends, Alex, is a TikTok influencer. Every time a new comment appears on his latest video, “A Day in My Life”, he cannot resist he immediately stops peeling to check and respond. Alex sure needs some help with self-control :)

Say, Alex, was the first person to get the only knife available and began peeling potatoes; and while peeling his second potato - “dinggg!!”, — a notification sound for a new comment on his video. He excitedly drops the knife and goes to check his phone.

While Alex is gone, the next person, Bayo, picks up the knife and starts peeling - no time to waste time! Say under a minute, Alex returns while Bayo is still peeling their first potato. As Alex was using the knife first before he was interrupted, Bayo hands the knife back to Alex.

With this approach, even though Alex and Bayo are not both peeling potatoes at the same time, they are still making progress at the same time:

  • Alex has made some progress: He is peeling his second potato

  • Bayo has also made some progress: He is peeling his first potato

Another word for ‘at the same time’ is simultaneously, so we can say that Alex and Bayo are making progress simultaneously.

In this analogy, the five friends represent five processes that need the processor (the knife) for them to carry out their task (peeling the potatoes). Since we have just one knife - the processor, it means we have a single-processor system.

This approach to processing is called Concurrency: where multiple processes appear to make progress at the same time. Note that at no point in time are two processes running at the same time, one has to suspend processing before another can start — friends Alex and Bayo are not peeling potatoes at the same time — there is only one knife! (Don’t worry, we will learn about what it means to run in parallel shortly.)

This highlight a very important requirement for processes to run concurrently: We must be able to suspend a process and resume it later for it to be able to execute concurrently.

When Alex let go of the knife to check new comments on his TikTok video, he changed context - from peeling potatoes to reading comments. In processing terms, we say that a context-switch happened. And a process that can be suspended is said to be preemptible. Preemptible is a fancy word that means “something can be taken over, or supplanted”.

Fantabulous!

Although there are no two active processes at any given time, execution of processes are interleaved

Parallelism

Let’s switch it up. Instead of having only one knife, imagine there are 4 knives! This means that all five friends do not have to wait for each other to start peeling the potatoes. Alex, Bayo, Caleb, and Dipo can start peeling their potatoes at the same time, while Eve waits for a knife to be free. We say that Alex, Bayo, Caleb, and Dipo, are processing in parallel.

For processes to run in parallel, there must be more than one processor available! Otherwise, the best they can do is run concurrently. Phew!

Parallelism means simultaneous execution of multiple tasks — this requires multiple processors or cores. While Concurrency means tasks progress independently.

Note that in this example of four knives and five friends (Alex, Bayo, Caleb, and Dipo are using the knives to start with), if Alex switches context again, then Eve can pick up the knife left by Alex to start peeling their potatoes while Alex is away. And if Alex returns before Eve is done, Alex may collect the knife back to continue his work. This means that we can have concurrent execution and parallel execution in a system at the same time.

Processes A, B, C, and D are running in parallel

Take away

The key lesson here is that when we write concurrent code (e.g using goroutines in Go, threads in Java, async/await in Python and JavaScript, or std::thread in C++), we are structuring our program to allow multiple tasks to make progress independently. The system resources will determine if they will run in parallel or not.

We write concurrent code with the expectation that it may run in parallel if system resources allow.

Concurrency is how a single-core computer can have multiple programs running - it’s an illusion! The operating system (OS) rapidly switches between programs, giving each a brief turn to use the CPU (a process called "time slicing"). This switching happens so quickly that it appears as if all programs are running simultaneously.

Again, fantabulous!!

Parting Words

If you enjoy this post, then you will enjoy other posts from me here on my blog. I also have a youtube channel and I plan to create more videos this year, (upcoming inFLUenCer? :) )

Happy Coding