Dark Hydra
Dark Hydra

Reputation: 275

Async/await multi core

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.

Upvotes: 3

Views: 4536

Answers (4)

Guffa
Guffa

Reputation: 700272

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.

Ref: Running Queries On Multi-Core Processors

Upvotes: 0

usr
usr

Reputation: 171188

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.

Upvotes: 0

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

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

Upvotes: 9

Patrick Hofman
Patrick Hofman

Reputation: 156958

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.

Upvotes: 1

Related Questions