When you go interview for a C# developer position, one question we’re bound to run into is: explain how concurrent processing (concurrent) and parallel processing (parallel) are different. And at a simpler level, the question might be worded a bit differently, namely: explain concurrent processing and parallel processing so a fifth grader can understand =))). Within the scope of this post I’ll try to explain it so that folks around 20 and up get it first, and for the younger crowd I’ll do my best haha.
Note that within the scope of this post, I’m only talking about parallel and concurrent processing in programming languages, not about operating systems (although there’s a bit of overlap in there). Oh, and before we get to explaining those 2 concepts, let’s look back at how asynchronous and synchronous programming work.
Updated (2024-10-02): I wrote a post about multitasking on the CPU and the way concurrent processing works at the operating system level here A few notes on the OS - single-tasking vs multitasking
Synchronous programming
When we write a program and run it, the program runs sequentially from top to bottom, line by line, until there’s nothing left and it stops. For example.
1 | function nauCom() { |
Here nauCom runs first and prints ‘Nấu cơm’, then ruaChen runs and prints ‘ruaChen’. Nothing difficult about that, right.
Imagine this is the work you have to do tonight. First you finish cooking the rice completely, and only then do you go wash the dishes. One task at a time until it’s done.
Asynchronous programming
Now with asynchronous programming, your code will be understood by the compiler as a task (a job) that needs to be carried out. The program still runs from top to bottom, but it will NOT wait for nauCom to finish before starting ruaChen, it runs them concurrently, meaning together, and it doesn’t even know which one will finish before the other (in other words, the tasks don’t depend on each other).
Keep imagining like above. You cook the rice and take the chance to wash the dishes at the same time, instead of sitting around waiting for the rice to be done and only then starting on the dishes.
Concurrent processing
Concurrent processing is when you, on your own, do many tasks at the same time. It’s really a behavior, a way of working to achieve asynchronous processing. For example, you go in with the mindset of doing several things at once so that no task has to wait on another. At this point, while you’re washing the vegetables, you run off to wash the dishes, wash a few of them and then go cook the rice… until everything is finished. That’s how you do it.
An example inside our very own computer. When a computer only has 1 core, how do you get to listen to music and write notes in notepad++ at the same time? At this point that one core is like us in the kitchen, it switches over to process a small chunk of the music then switches back to process the text we just typed, switching back and forth to handle them, but because the speed is so fast, we get the feeling that the CPU is focused on doing one single thing. How much of one task a computer core processes before switching to another task depends on that machine’s operating system.
Parallel processing
Parallel processing is when you, plus one or more other people, do many tasks at the same time. Continuing with the example above, instead of you alone in the kitchen running back and forth handling the pile of work, now you have allies, each person takes on one task and everyone works together. This way of working is called parallel processing.
In summary
In synchronous programming the code runs sequentially, so there’s no concurrent or parallel processing here.
Parallel processing is when two or more tasks run at the same point in time, possibly identical down to the millisecond, and it can only be achieved with multi-core processors.