`co_yield` 可以在协程恢复时从调用者返回一个值吗?

J. *_*rez 4 c++ coroutine c++20

C++20 引入了协程,可以用来创建生成器和其他类似的东西:

generator<int> counter(int max) {
    for(int i = 0; i < max; i++) {
        co_yield i;
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以创建一个协程,以便调用者可以提供一个响应,该响应co_yield在协程恢复后返回?让我们称其为 achannel而不是生成器。

这是我希望能够做的一个例子:

channel<int, int> accumulator(int initial) {
    while(true) {
        // Can channel be written so co_yield provides a response?
        int response = co_yield initial;
        initial += response;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,每当调用者恢复协程时,它都会提供一个值,co_yield一旦协程恢复,该值就会返回,如下所示:

std::vector<int> accumulate(std::vector<int> values) {
    channel<int, int> acc = accumulator(0);

    std::vector<int> summed_values;

    for(int v : values) {
        // Get whatever value was yielded by the accumulator
        int sum = acc.recieve();
        // Do something with the value
        summed_values.push_back(sum);
        // Resume the accumulator, returning this value from co_yield:
        acc.send(v); 
    }
    return summed_values;
}
Run Code Online (Sandbox Code Playgroud)

根据评论编辑

任何人都可以提供有关如何执行此操作的一些指导或示例吗?协程对我来说仍然很陌生。我有一个channel类的基本实现,但我不确定应该返回什么yield_value才能实现这一点。

问题我已标记的两个位置(A),并(B)在评论中。

template <class Out, class In>
struct channel {
    struct promise_type {
        Out current_value;
        auto yield_value(Out value) {
            current_value = value;
            // (A) What do I return here?
        }
        channel get_return_object() {
            return {std::coroutine_handle<promise_type>::from_promise(*this)};
        }
        // We run up until the first value is ready
        auto initial_suspend() noexcept { return std::suspend_never(); }
        auto final_suspend() noexcept { return std::suspend_always(); }
        void unhandled_exception() noexcept { std::terminate(); }
    };


    Out receive() {
        return handle.promise().current_value;
    }
    void send(In response) {
        // (B) What do I do here?
    }
    // Constructors, destructor and move assignment operator omitted for brevity
   private:
    std::coroutine_handle<promise_type> handle = nullptr;
};
Run Code Online (Sandbox Code Playgroud)

cpp*_*ner 5

关键是await_resume,在等待者(的结果yield_value)上调用它来获取 的结果co_yield

您还需要将响应存储在某处。正如 Raymond Chen 在评论中建议的那样,您可以将该值放在promise_type.

所以变化是:

  1. 将数据成员添加到promise_type.

    In response;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 定义一个自定义的等待器以返回该数据成员。

    struct awaiter : std::suspend_always {
        friend promise_type;
        constexpr In await_resume() const { return m_p->response; }
    
    private:
        constexpr awaiter(promise_type* p) : m_p(p) {}
        promise_type* m_p;
    };
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在 中(A),返回自定义的等待器。

    return awaiter(this);
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在 中(B),设置数据成员,然后恢复协程。

    handle.promise().response = response;
    handle.resume();
    
    Run Code Online (Sandbox Code Playgroud)