Difference between multithreaded and asynchronous program in C #

At first I searched on Stackoverflow and google for a similar type of question. Only one link gave some points, but I cannot understand clearly. [ 1 ]

The following questions haunt me:

  • In asynchronous programming, what is the real callback method? is delegate?

  • Does the async nested program use multiple threads?

If they provided me with a schedule, I would be very grateful




[ 1 ] "The difference between multithreaded and asynchronous programming"

+9
multithreading c # asynchronous
Apr 17 '15 at 10:13
source share
4 answers

1) The callback is basically a delegate passed to the procedure that this procedure will β€œcall back” at some suitable point. For example, in asynchronous calls, such as WebRequest.BeginGetResponse or WCF BeginXxx operations, you must pass AsyncCallback. The worker will "redirect" any method that you pass as an AsyncCallback, in which case, when it finishes, to inform you that it is finished and get the result.

2) Multithreading is different parts of an executable program, typically called threads.

Asynchronous programming uses threads to run a piece of code. Thus, asynchronous programming relies on multithreading to work. See the link below:

Asynchronous Programming and Multithreading

The difference between multithreaded and asynchronous programmers

+2
Apr 17 '15 at 10:21
source share

Single thread lock

To understand asynchronous / parallel / multithreaded, we need to start from the very basics and WHY we have added so many difficulties to the problem.

In the beginning, there were only single-threaded applications for locking. These are really simple programs, and most likely this is what you are writing right now.

To explain, I will use the pizza house analogy.

Imagine your computer is a one-person pizza house. He had only basic training.

You go to the store, talk with the employee, look at the menu and order pizza.

Great. An employee enters the back of the store, puts pizza on top and stands next to the oven, waiting for the pizza to cook .

Do you remember that your wife does not like pineapples. You are shouting at an employee, trying to attract his attention to change your order. No dice. He will not move from the stove (he fell asleep).

You get annoyed and leave.

Multithreaded parallel blocking

You go to the next pizza shop.

It has 4 employees.

You go to the store, talk with the employee, look at the menu and order pizza.

He screams (sends a message) to other employees in the back to make pizza. They put one in the oven. He stands next to the oven and sleeps.

You forget that your wife is allergic to bacon. You tell employee A to cancel the pizza. He screams inside the cook, wakes him up. The cook throws the pizza into the hopper and puts the kosher pizza in the oven. He quickly fell asleep.

Wait, the pizza is ready, you will get the bill. Its massive (hiring too many employees, and half of them sleep at work).

Single-Threaded Asynchronous / Non-Blocking

You go to the next pizza shop.

He has 1 employee.

You go to the store, talk with the employee, look at the menu and order pizza.

He goes inside, puts the pizza in the oven. He then attaches a receipt ( callback ) to the pizza. He returns to the counter.

Do you remember that your wife didn’t really eat meat this month. You tell the employee, he goes inside, fixes the situation.

After the employee checks inside for the made pizza. Then he reads the receipt (give this pizza to Bob).

You get a cheap affordable and responsive pizza.

+2
Mar 01 '17 at 8:13
source share
  • Asynchronous programming is the ability to execute part of the code parallel to the main program flow. These can be web service calls or any task in the current application that is running. The callback is either a named or anonymous method that can be introduced by the delegate. The callback may return with a result or exception. Since the results from asynchronous methods are returned at any time, if the results of asynchronous methods are requested, the current executable thread is blocked until the method returns with some exception or result. .Net 3.0 used BackgroundWorker, BeginInvoke / EndInvoke and IAsyncResult (still in use) .net 4.0 has Tasks .net 4.5 asynchronous wait to accomplish this

  • Asynchronous calls are made using multithreading. Based on an implementation, for example, if Tasks are used to do some work asynchronously that uses threadpool threads, then the structure will determine whether the code will execute in the current thread or whether new threads will be required.

0
Apr 18 '15 at 10:20
source share

The difference between multi-threaded and asynchronous programming is that in multi-threaded we create a new thread for a function that performs or performs only this function or task

asynchronous programming also uses multithreading, but in another way, in async, a work or task is divided into several threads

for example, if we have 4 tasks, if we use multithreading and assign threads as follows

Thread1 = task1 Thread2 = task2 Thread3 = task3 Thread4 = task4 

but when we use the asynchronous programming model we do not need to assign threads

A task

automatically shared between threads maybe it uses a single thread or multiple threads as needed

if he will use one thread, then task 1-4 will work simultaneously

in circular mode

switching the processor between tasks, starting the task takes several seconds, and then saving the position "switching context", and then starting another task. it happens so fast and it seems an illusion that all tasks work simultaneously

similarly, when we have multiple threads in an asynchronous model

one task is a handle to several threads, for example, if task1 is started by Thread1, it can be started on thread2 or thread3 when thread1 does not work on task1

This is the beauty of asynchronous programming.

I think this is the best explanation for beginners.

https://codewala.net/2015/07/29/concurrency-vs-multi-threading-vs-asynchronous-programming-explained/#comment-21276

0
Mar 01 '17 at 7:42 on
source share



All Articles