Race Condition in Async/Await Code

后端 未结 2 514
孤街浪徒
孤街浪徒 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:16

    There are a number of things that could be going on here.

    For one, what sort of executable are you running? When Await fires, it uses the current synchronization context, so your awaited code could be getting serialized to 1 a UI thread.

    Also, since there is no memory barrier/volatility protection around the variable, your threads might be reading indiviually cached values (as mentioned by @Spo1ler in his post)

    Also, the thread pool might be choosing to run both your requests on the same thread (it's within its rights to do so -- you're letting .net/windows decide when and how to allocate threads)

    Bottom line though, you really should be protecting the access to the variable with synchronization or interlocked operations.

提交回复
热议问题