【问题标题】:What is the actual implementation of co_await in c++20 co-routinec++20协程co_await的实际实现是什么
【发布时间】:2020-07-26 23:29:09
【问题描述】:

我正在研究 C++20 中的协程。

我试图了解 co_await 是如何工作的。

是 co_await 的任何实现。

阅读,co_wait似乎有future/promise的行为。

在线程内使用 co_wait 调用函数/对象时,会调用类似这样的内容

auto promise = std::promise<std::string>();
auto future = promise.get_future();
void sleep()
{
     std::cout << future.get() << std::endl;
}

当 notify 被调用时,另一个线程调用类似这样的东西

void wakeup()
{
     promise.set_value("Hello World");
}

【问题讨论】:

标签: c++ asynchronous c++20 c++-coroutine


【解决方案1】:

例如TYPE a = co_await awaitable(args), 好像是这样展开的:

    {
        auto && tmp = awaitable(args);
        if (!tmp.await_ready())
        {
            tmp.await_suspend(*this);
        }
        ret = tmp.await_resume();
    };

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 2017-09-16
    • 2019-07-31
    • 2014-06-30
    • 2023-04-05
    • 2019-05-27
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多