c# - Understanding async/await without threads -


according msdn, async , await not create new threads:

the async , await keywords don't cause additional threads created.

with in mind, i'm having difficulty understanding control flow of simple programs. complete example below. note requires dataflow library, can install nuget.

using system; using system.threading.tasks.dataflow;  namespace tasksandbox {     class program     {         static void main(string[] args)         {             bufferblock<int> bufferblock = new bufferblock<int>();              consume(bufferblock);             produce(bufferblock);              console.readline();         }          static bool touched;         static void produce(itargetblock<int> target)         {             (int = 0; < 5; i++)             {                 console.error.writeline("producing " + i);                 target.post(i);                 console.error.writeline("performing intensive computation");                 touched = false;                 (int j = 0; j < 100000000; j++)                     ;                 console.error.writeline("finished intensive computation. touched: " + touched);             }              target.complete();         }          static async void consume(isourceblock<int> source)         {             while (await source.outputavailableasync())             {                 touched = true;                 int received = source.receive();                 console.error.writeline("received " + received);             }         }     } } 

output:

producing 0 performing intensive computation received 0 finished intensive computation. touched: true producing 1 performing intensive computation received 1 finished intensive computation. touched: true producing 2 performing intensive computation received 2 finished intensive computation. touched: false producing 3 performing intensive computation received 3 finished intensive computation. touched: false producing 4 performing intensive computation received 4 finished intensive computation. touched: true 

this seems indicate consume given control while for loop running, outputavailableasync task completes:

for (int j = 0; j < 100000000; j++)     ; 

this unsurprising in threaded model. if no additional threads involved, how can produce yield control in middle of for loop?

if no additional threads involved, how can produce yield control in middle of loop?

who said no additional threads involved? fact stated was:

the async , await keywords don't cause additional threads created.

which absolutely true. program includes fragments

target.post(i);  await source.outputavailableasync()) 

my guess call target.post(i) or source.outputavailableasync() created thread. await doesn't produce thread; await assigns remainder of method continuation of task returned call , returns control caller. if task spawned thread work, that's it's business.

await control flow; complicated control flow, sure, control flow nevertheless. it's not syntactic sugar creating threads; it's syntactic sugar assigning continuation task.


Comments

Popular posts from this blog

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -

python - How to create a legend for 3D bar in matplotlib? -