Async programming vs Parallel programming (Basic)

Shayan Kamalzadeh
3 min readMay 6, 2022

Today I want to talk about asynchronous programming. What is async programming and why is important? and what is the difference between async programming and parallel programming?

Imagine you have a computer with a CPU, Two processes, and a thread. threads inside each process.

Question: What is the difference between process and thread?

when you start two different programs like the .net application, one program cannot access the memory of the second, so we have a kind of process isolation boundary. But the thread has another story: each process can have multiple threads and these threads can access all the memory of the entire process, therefore you can create a list of something and if you have 5 threads, these threads can all access the same list of something.

Now Imagine you have several processes and just 2 or 3 CPUs.Who is responsible for running all these processes.

Yes, the Operation system scheduler them. The processes are distributed across all the CPUs. and now thinking about a c# application that you wrote and it has some heavy calculation or doing some rendering with CPU like a game application or mathematical calculation. If you do that you typically split up your work into multiple independent units, you write the c# program, and these distribute the calculation problem into multiple parts, and you run them in parallel .therefore what we have for a certain amount of time in the CPU one, running certain thread let’s call it the thread one and at the same time you have on the CPU 2 running another thread(two) and at the end, you are maybe getting the resulting data, and transfer data to another thread for another CPU. This is Parallel programming.

Parallel programming means that you have some CPU-bound stuff that you would like to do really in parallel at the same point in time, multiple calculations are going on in your CPU. Be careful this is not Async programming.

For Parallel programming, we need multiple CPU Cores, but for Async programming, we do not need multiple CPU Cores.

So we can think we have a CPU on our computer and run windows or WPF application. and our CPU has one thread for some part that we work this application, our thread is working and not idle, now imagine you won’t fetch a huge data from another server, so you send a request to the server and for example, a SQL engine in server start to fetch data, if you do not use async programming, our thread after sending a request to the server, waiting for a response and if you have just one thread like our scenario, your application freeze until getting a response.

As you can see in the above figure, my thread for 10 seconds was idle, and it is not good for our application. In async programming, when a thread sends a request for another server to get data, is freed and it can do other jobs and when data is prepared, a thread is responsible for bringing the data.

--

--