0

Is it a bad idea to do this?

var result = await Task.Run(CPUBoundWork);
NotAwaitedCPUWork();

Is that because the work that follows the await statement, i.e. the continuation, which in this case would be the method call to NotAwaitedCPUWork would simply contest for the CPU with the awaited work represented by the task that has already been scheduled, and this would unnecessarily put load on the task scheduler to inject more threads or queue up the work on an existing thread; as a result, the continuation will anyway have to wait until a thread becomes available?

If this explanation is the most plausible one, then what is the correct way to run CPU bound work with a task assuming that you do care about the result of the CPU-bound work? Do you just block the current thread by Task.Wait()ing on the CPU-bound work?

What is the recommended practice for awaiting the completion of CPU-bound work when you care about the result of the work?

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • 2
    Did you have a look at: http://stackoverflow.com/questions/14896856/is-async-await-suitable-for-methods-that-are-both-io-and-cpu-bound ? – Noctis Jun 05 '16 at 00:20
  • Beautiful. Thank you. That answers my question. Note to self: Stephen Cleary's is a blog to read from A to Z. – Water Cooler v2 Jun 05 '16 at 00:26
  • Yeah, he is. He usually pops up when looking at concurrency :) . If I had the time, i would read his blog A to Z as well. probably would do the same with some other ones as well :) – Noctis Jun 05 '16 at 01:00

0 Answers0