三人行,必有我师矣!读oldnewthing笔记。
201912 C++ coroutines: The problem of the DispatcherQueue task that runs too soon, part 4
不使用同步原语,而是改成:
bool await_suspend(coroutine_handle<> handle)
{
// m_queued =
return
m_dispatcher.TryEnqueue([this, handle]
{
m_queued = true;
handle();
});
// return m_queued;
}
201912 C++ coroutines: The problem of the DispatcherQueue task that runs too soon, part 3
将同步原语作为awaitable的成员
201912 C++ coroutines: The problem of the DispatcherQueue task that runs too soon, part 2
使用下面的设施构造同步原语:
- std::atomic_thread_fence
- std::atomic_thread_fence(std::memory_order_acquire);
- std::atomic_thread_fence(std::memory_order_release);
- WakeByAddressAll
- WaitOnAddress
201912 C++ coroutines: The problem of the DispatcherQueue task that runs too soon, part 1
bool await_suspend(coroutine_handle<> handle)
{
m_queued = m_dispatcher.TryEnqueue([handle]
{
handle();
});
return m_queued;
}
bool await_resume()
{
return m_queued;
}
注意,handle();
有可能在m_queued=...
之前运行。await_resume()
会返回错误值。
(本篇完)