Race Condition in Async/Await Code

后端 未结 2 495
孤街浪徒
孤街浪徒 2021-01-04 19:59

I just wonder whether a race condition occurs in the code below:

int readingFiles;
async Task ReadFile (string file)
{    
    ++readingFiles;
         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 20:11

    There is no race condition here. The .NET runtime will insert the appropriate memory barriers.

    Also see the comments on: http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/async-await-faq.aspx

    Yes, TPL includes the appropriate barriers when tasks are queued and at the beginning/end of task execution so that values are appropriately made visible.

提交回复
热议问题