Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the relation between Asynchronous and parallel programming in c#?

I am getting confused as asynchronous programming is a way to execute a block of code asynchronously, that calls a method and doesn't wait for the result. In the same way, parallel programming is a way to execute more than one task simultaneously, but all those tasks are executed asynchronously. So wondering/confused what is the relationship between these two programming paradigms in c#.

like image 427
Alagesan Palani Avatar asked Sep 15 '12 03:09

Alagesan Palani


People also ask

Is asynchronous programming same as parallel programming?

Asynchronous programming involves some calculations time-intensive tasks, which on the one hand are engaging a thread in the background but do not affect the normal flow of the program. Parallel programming incorporates several threads to perform a task faster and so does concurrent programming.

What is the difference between asynchronous and parallel?

When you run something asynchronously it means it is non-blocking, you execute it without waiting for it to complete and carry on with other things. Parallelism means to run multiple things at the same time, in parallel.

What is asynchronous in parallel computing?

Abstract. Asynchronous parallelism is the most general form of parallelism, whereby processors operate freely on tasks, and global synchronization is not required. In place of the lockstep fashion of the various forms of synchronized parallelism, the asynchronous method relies on locks and explicit control flow.

Can we achieve parallel programming through async and await?

so an asynchronous programming implemented using async and await in c# 4.5 can also be considered parallel programming? I would say yes.


6 Answers

Parallel programming is a technique where we use multiple threads to execute a task faster. This means that on modern multi-core architectures we can utilize more of the resources available to perform a task.

A great example of this is sorting a list using quicksort.

Normally with parallel programming, performance is important and all the threads are working to a common goal.

Asynchronous programming is subtly different. This normally involves longer running tasks and tasks which are perhaps waiting on some kind of external stimuli. A good example of this is to perform a large calculation in a background thread so that the UI remains responsive. With asynchronous code we are normally talking about code which executes at a different rate to our main application.

like image 194
Not loved Avatar answered Sep 22 '22 06:09

Not loved


Parallel programming means executing operations At the same time using multiple threads, processes cpu's and or cores.

Asynchronous programming as you said means to fire a request and provide a callback mechanism to receive the response.

like image 30
Tutan Ramen Avatar answered Sep 20 '22 06:09

Tutan Ramen


finally :

use Parallel programming for CPU Intensive solutions. use Asynchronous programming for IO Bound solutions.

like image 32
Sonador Avatar answered Sep 23 '22 06:09

Sonador


In general asynchronous means execute when ever possible, Parallel means execute right away by creating a new execution thread.
Here the link

like image 41
diyoda_ Avatar answered Sep 19 '22 06:09

diyoda_


One of the most simplest ways I was able to understand Parallel and Asynchronous programming was by thinking of the "boiling an egg" scenario taken from Pluralsight which I've changed slightly to incorporate threads.

Parallel Programming

  • Hob: CPU
  • Multiple Pots: Thread
  • Multiple Eggs: Parallel Task

You have multiple eggs (Tasks) which need to be boiled at the same time. In this example, the hob will be the CPU, each pot which is boiling one egg will be a single thread and the eggs are the parallel task. I can boil multiple eggs (Tasks) by adding more pots (Threads) to the hob (CPU) at the same time. Without the use of parallel programming, I could only boil 1x egg (Task) at one time as you would only have 1x pot (Thread) to work with which would ultimately slow down the process of boiling the eggs.

Asynchronous Programming

  • Egg Timer: Asynchronous Task
  • Hob: CPU
  • Multiple Pots: Thread
  • Multiple Eggs: Task

Following on from the example above, you now decide that you only want know when all of the eggs have been boiled rather than when each single egg has finished boiling. To do this, you would use an asynchronous task which in this instance will play as the egg timer. The egg timer (Asynchronous Task) will update you when the all of the eggs (Tasks) have been completed so your free to do other things up until this point.

like image 43
Kitson88 Avatar answered Sep 21 '22 06:09

Kitson88


Parallel programming is mostly concerned about improving PERFORMANCE of the system.

Asynchronous programming is mostly concerned about improving RESPONSIVENESS of the system.

Threads, tasks etc. are techniques to achieve both async and parallel programming.

like image 40
mandar Avatar answered Sep 19 '22 06:09

mandar