Does async/await should be used with threads to utilize multi core? I understand async/await incompletely, but looks like it doesn't create new threads and doesn't use thread pool. So it runs code on current thread, that means no multi core support.
-
3Async/await per se is not about threads. It's about suspending the execution awaiting for the result, and releasing resources for other code. Threading is kind of external to this feature: the implementation may decide to switch to another thread for executing the task. (And `Task.Run` will use a thread pool thread AFAIK, too. But it's not directly related to async/await feature.) – Vlad Aug 31 '14 at 12:13
-
Correct. It is about the IO pool, not the thread pool. – Patrick Hofman Aug 31 '14 at 12:16
4 Answers
async-await
isn't about load-balancing work across multiple cores. It is about taking advantage of operations that are asynchronous by nature and releasing resources to process more work while at it. Good asynchronous APIs dont use extra threads to execute work. Usually, There is no Thread, meaning code will continue to execute on the same thread until hitting the first await
and yield control back to the caller.
You can look at examples of async APIs such as HttpClient
, StreamWriter
, SmtpClient
, etc. They all process work over the wire (network driver calls, disk drive calls, etc).
If what you're looking for is parallel processing, look into the Parallel
class.
You can also start by reading Parallel Programming in the .NET Framework

- 146,575
- 32
- 257
- 321
-
If There is no Thread, then why does the C# example shown in the second answer to the following question demonstrate async await spawning multiple threads? https://stackoverflow.com/questions/38865050/is-await-in-python3-cooperative-multitasking/39024906 – Ozymandias Sep 04 '18 at 01:12
-
@AjaxLeung Because when you use `Task.Delay` in a console application, what backs up the synchronization context is a thread pool, which just resumes execution on an arbitrary thread. does not spawn any threads Try fhat same example in a WinForm app. – Yuval Itzchakov Sep 04 '18 at 05:50
According to MSDN:
The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.
That's all it does. It doesn't have anything to do with threading at all. It will just start doing something else on the same thread until the other task (not Task
) has completed. async / await will also befenit performance on single core systems.

- 153,850
- 22
- 249
- 325
If you try to use async/await like that, it will just start a new thread, but it can't be used to run something on multiple cores. The work is simply transfered to the other thread and frees up the current thread.
You would use something like PLINQ to do work on multiple cores.

- 687,336
- 108
- 737
- 1,005
-
2Strictly speaking, async/await doesn't start a new thread, it's a Task-impleneting method that does. – Vlad Aug 31 '14 at 12:20
-
@Vlad: Good point, I reworded that part to try to avoid that distinction. – Guffa Aug 31 '14 at 12:33
-
@Vlad: Not good enough? Do you really think that the answer warrants a downvote because of a detail that isn't really relevant? – Guffa Aug 31 '14 at 12:36
-
1The afirmation that "async/await will normally make a new thread start" is not relevant? I've read some documentation saying otherwise, so for me is at least confusing. – Xavier Egea Aug 31 '14 at 12:53
-
3I have never heard that the standard framework async functions start a new thread or even a new task. They are often IO methods that use other means of asynchronous calls, like IOCPs. – Dirk Aug 31 '14 at 13:16
-
@Dirk: That is a different matter. That's not what the OP is trying to do. – Guffa Aug 31 '14 at 13:50
-
@Freerider: What would you like that it said instead? If you try to use async/await like the OP is talking about, it will definitely start a new thread. – Guffa Aug 31 '14 at 13:52
-
@Guffa I beleive you. I'm only confused about other responses on current question saying otherwise. "It doesn't have anything to do with threading at all", "There is no thread" are some sentences extracted from other responses on this question. As I understand you are not agree with them. – Xavier Egea Sep 01 '14 at 07:26
-
@Freerider: If you are doing something that involves waiting time, like IO operations or web requests, then it doesn't have to start a new thread, at least not until there is some work to do. When you just want to start a task right away, which is what the OP is talking about, it has to start a new thread for that to go somewhere. Using async/await is actually often proposed as a solution to do work in the background to keep the UI responsive. – Guffa Sep 01 '14 at 08:15
-
There is nothing inherent in await
forcing non-parallel execution. You can easily start multiple tasks, run them in parallel and await them all at once.
await
does ensure that the async
method runs in the ambient synchronization context that is active. This is usually something that tends to force non-parallel execution. All mainstream synchronization contexts are non-parallel. You can break out of that limitation at will by using Task.Run
.
That said, in GUI apps you don't need parallel execution on the UI thread because you shouldn't do much work there at all.
In server apps the sync context is single-threaded per request. This is also a fine default. Different requests run in parallel.
And again, you can break out at will:
var t1 = Task.Run(...);
var t2 = Task.Run(...);
await t1;
await t2;
This runs in parallel.
var t1 = Task.Run(...);
await t1;
var t2 = Task.Run(...);
await t2;
This runs serially.

- 168,620
- 35
- 240
- 369