Differences Between Multithreaded, Asynchronous, And Parallel Programming

The availability of multi-core processors in modern times has brought out concurrent programming, which has become an even more pressing issue in recent years. The goal of concurrent programming is to be able to do a lot of work in a short amount of time.

What is the difference between MultiThread, Asynchronous, and Parallel programming?

The question is not so easy... In many C# books, you read in English, the issue of multicore programming starts with Multithreading. It's just that in many of those books, there is no place for this question.

Multithread programming

The main goal in multithreaded programming is to run the smallest units of work (methods) by packaging them into Threads. This allows the methods to run in parallel, if possible, or in an intermittent form within quantum time.

If your computer consists of 4 cores, your computer can really (absolutely) only do 4 things at the same time! But what if I create 10 Threads? Most likely not many of those threads will be running at the same time. Because physically, 1 method can work at the same time in 1 core. When we turn on the computer, not all of these cores are idle. Because the operating system itself has several services that require a kernel to work.

So what does multithreading do? Wrapping our methods in the Thread class ensures that they can access the core and run there. Multithreaded programming is not parallel programming! Only in parallel programming, it is possible to ensure that several jobs are physically done at the same time. Multithreaded programming in one is not designed to run on different cores. There is a parallel program for this. When you use old-school Threads, you are running Multithreaded programming. Currently, the Thread API is not widely used. We use Tasks, which are smaller units of work. (I will explain the difference between Thread and Task in one of the upcoming articles)

Multithreading works with quantum time. Quantum time is a time slot randomly allocated to any Thread. It can be 5-10-15 etc. nanoseconds. It can be more. Each Thread is coordinated by the ThreadManager and sent to the kernel for processing. After the quantum time allocated to it is over, the current state of the Thread is stored in TLS(Thread Local Storage) and the Thread is temporarily frozen, the next Thread is sent to work. When the queue is delivered back to the frozen Thread, it resumes its work by recovering its data from TLS. After the Thread completes its work, it is removed from the coordination list. That is, the Thread is released to do each job. If you have 10 methods, then you will have 10 Threads. Multithreading works roughly this way.

The differences between Multithreaded, Asynchronous, and Parallel programming

Asynchronous Programming is the process of performing tasks asynchronously. Before moving on to asynchronous programming, let's explain "synchronous and asynchronous concepts". Synchronous execution of a task means that the team does not move on to the next task before the previous task is finished! In asynchronous programming, one job can start and work before it is finished, and a parallelism effect is created. Often novice programmers think that multithreading is the same as asynchronous programming! But these are completely different concepts. We use asynchronous programming mainly when connecting to a database, when using some services from the Internet (networking), when reading data from a physical disk, etc. As many know, asynchronous delegates (APM), EAV, and Task API (TPL) are also mainly intended for asynchronous programming processes. Because many developers use asynchronous programming in a wrong way, they get the Multithread effect and think that asynchronous programming creates a Thread! Asynchronous programming does not create a Thread!!

The differences between Multithreaded, Asynchronous, and Parallel programming

If you take a task that requires processor time in Asynchronous API, of course, a new Thread will be created! We should only use asynchronous programming APIs for asynchronous tasks.

The differences between Multithreaded, Asynchronous, and Parallel programming

So, if any work requires processor time (performs some calculation, consumes processor resources using for, for each), then we should use multithread or parallel programming (depending on the amount of work). If any operation does not require processor time, If it uses disk, internet, database, etc., then an asynchronous model can be used.

Parallel programming

Parallel programming is performed through the Parallel API in C#. If any method performs long-term work, that work is divided into small parts and given to separate cores. This way the method works faster. For example, if you have built a cycle up to 10 billion, it takes a long time to wait for it to be executed in one core. Parallel programming divides the method into smaller Tasks and transmits them to the cores, and the operation takes place faster and in the same time interval.

Similar Articles