Reputation: 438
I was trying to understand the difference between Async
programming and Multi Threaded
programming. In async
we say that thread takes a task(T1) and start executing it. If at the same time another task(T2) comes its way, it is NOT going to wait for T1 to finish, rather it will pick the second task T2 while T1 executes in the background.
The highlighted part is the most confusing part for me to wrap my head around. So when thread picks up T2, who is executing T1? What is the role of a thread in Operating System context? What does it do exactly?
This might be stupid question but my hours long ordeal on internet did not yield satisfaction. Or Am I just asking the wrong question?
Upvotes: 2
Views: 213
Reputation: 1245
Actually, the confusion comes from the idea of task T1 being executed in the background. The scenario you are writing about is only possible if T1 is an IO-bound task so it doesn't occupy a thread (so it's not executing, but waiting for the results from some IO device driver) when the IO operation is running. In that case the thread which is not busy and can pick some other work.
In case you have a pure CPU-bound task it always needs a thread to run on. If you want to offload this operation to a ThreadPool thread (for example, in order to keep your UI responsive by offloading work from the main UI thread), a common pattern is using Task.Run:
await Task.Run(/* your CPU-bound method call here */);
I find explanations by Stephen Cleary extremely useful when it comes to async / await, Tasks, TPL etc. So, here are the links you might want to follow for more detailed explanation:
In my opinion, it always pays off when you dig one level deeper, which in case of async / await will be classic threading. There is an awesome (and free) Threading in C# by Joseph Albahari. There you'll find all important details about threading, synchronization, parallel programming.
Upvotes: 2
Reputation: 1994
Presumably if by async
programming an asynchronous programming is meant it merely implies programming of a computation flow when some events may occur in not strictly defined order, period. From that standpoint both mechanisms: threads and I/O are asynchronous. However the .NET async
mechanism isn't directly related to them. In essence it's just a framework to wrap asynchronous-like computations in a form which would mimic synchronous execution but keep asynchronous semantic. That is, it doesn't control the fact whether underlying operation is asynchronous or synchronous (it's controlled by operation itself), even more it can successfully be applied to both types of operations but it's more beneficial to use it with asynchronous operations, that's probably why this mechanism is strongly associated by developers with asynchronous programming. So the answer to your question is "it depends". In case of I/O-bound asynchronous operation it's IO subsystem along with I/O device is something which can supply execution of T1 at the moment when T2 is picked up and being executed. In case of CPU-bound asynchronous operation it can be another thread. In case when operations are synchronous they will be executed one by one, that is synchronously.
Upvotes: 2