Marvin's Blog【程式人生】

Ability will never catch up with the demand for it

01 Feb 2020

读oldnewthing笔记(一):await_suspend以及DispatcherQueue

三人行,必有我师矣!读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

使用下面的设施构造同步原语:

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()会返回错误值。

(本篇完)

comments powered by Disqus