Are you there, asynchronously written value?

后端 未结 2 1197
轻奢々
轻奢々 2021-01-18 12:24

The last couple of days I\'ve been reading about async/await. Yesterday I found this video on Channel 9 what made wonder about some things. Please consider the slide below.<

2条回答
  •  囚心锁ツ
    2021-01-18 12:54

    In the above example, it is safe to read the m_GetResponse because assignment will happen in same UI thread given that this is being called from UI.

    This is because SynchronizationContext will be captured and continued when the async method resumes. So it is the same UI thread which writes the field and reading it. This isn't a problem here. Refer my related answer here

    If called from non UI context, there is no guarantee that continuations will be ran in same thread. Usually it will be ran in ThreadPool thread. Given that the field read isn't volatile it is possible that you could get the previous value if necessary barriers are not inserted. But you don't need to worry about it because TPL already does this for you.

    From the above link

    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

    So with TPL, you don't need to worry about memory barriers given that the Tasks are already completed. But if you're creating threads manually(which you shouldn't be doing) and dealing with threads directly --you'll have to insert necessary memory barriers.

    Btw, ReadToEnd is a blocking call. I would not call it in UI thread. I'd use ReadToEndAsync instead to make your UI thread free. And I'll not use field here; I'll return the value from async method because every method call is just dependent on the parameter so it makes sense to return the value from the method.

    So, your method will become something like the following

    private async Task SendDataAsync(string url)
    {
        var request = WebRequest.Create(url);
        using(var response = await request.GetResponseAsync());
        using(var reader = new StreamReader(request.GetResponseStream());
            return await reader.ReadToEndAsync();
    }
    

提交回复
热议问题