The series continue.
by Raymond Chen
From the article:
Last time, we made another attempt to fix a race condition in C++/WinRT’s resume_foreground(DispatcherQueue) function when it tries to resume execution on a dispatcher queue. We did this by having the queued task wait until await_suspend was finished before allowing the coroutine to resume, and we found a nice place to put the synchronization object, namely in the awaiter, but even with that fix, we introduced additional memory barriers into the hot code path.
But it turns out all this work was unnecessary. We just had to look at the problem a different way.
The purpose of storing the result of TryEnqueue into m_queued is so that await_resume can report whether the lambda was queued or not. But we can infer that information another way: The fact that our lambda is running means that got got queued. Because if the lambda were not queued, then it would never have run in the first place...