Reputation: 5394
If I have this
static void Main(string[] args)
{
var tasks = new List<Task>();
for (int i = 0; i < 10; i++)
{
tasks.Add(AsyncWithBlockingIO(i));
}
Task.WaitAll(tasks.ToArray());
}
private static async Task AsyncWithBlockingIO(int fileNum)
{
var result = await File.ReadAllTextAsync($"File{fileNum}").ConfigureAwait(false);
//will the below run concurrently on multiple threads?
CpuNoIOWork(result);
}
Will CpuNoIOWork()
run concurrently on multiple threads (using the thread pool) as the IO calls complete or will it only use 1 thread at a time?
Upvotes: 0
Views: 313
Reputation: 456687
Will CpuNoIOWork() run concurrently on multiple threads (using the thread pool) as the IO calls complete or will it only use 1 thread at a time?
It will run concurrently, on thread pool threads.
However, depending on the internals of what you're await
ing, it might continue on an I/O thread pool thread, which is not desirable. If you have CPU-bound work, I recommend wrapping that in Task.Run
, which will ensure the work will be run on a worker thread pool thread:
var result = await File.ReadAllTextAsync($"File{fileNum}").ConfigureAwait(false);
await Task.Run(() => CpuNoIOWork(result));
Upvotes: 7