設置 | 登錄 | 註冊

目前共有4篇帖子。

C++20 Coroutines协程

1樓 巨大八爪鱼 2026-6-5 17:13
【test.cpp】
#include <coroutine>
#include <iostream>

struct return_object {
    struct promise_type {
        return_object get_return_object() {
            return_object obj;
            obj.that = this;
            return obj;
        }
        std::suspend_never initial_suspend() {
            std::suspend_never never;
            return never;
        }
        std::suspend_never final_suspend() noexcept {
            std::suspend_never never;
            return never;
        }
        void unhandled_exception() {

        }
    };

    promise_type *that;
};

return_object counter() {
    std::cout << "counter begin" << std::endl;
    for (int i = 0; i < 10; i++) {
        std::suspend_always a;
        co_await a;
        std::cout << "counter: " << i << std::endl;
    }
}

int main()
{
    return_object r = counter();
    std::coroutine_handle<return_object::promise_type> h = std::coroutine_handle<return_object::promise_type>::from_promise(*r.that);

    for (int i = 0; i < 3; i++) {
        std::cout << "in main function" << std::endl;
        h();
    }
    h.destroy();
    return 0;
}

【编译命令】
g++ test.cpp -o test -fcoroutines

【程序运行结果】
oct1158@oct1158-ubuntu:~/Documents/Code/C/test/coroutine$ ./test
counter begin
in main function
counter: 0
in main function
counter: 1
in main function
counter: 2
oct1158@oct1158-ubuntu:~/Documents/Code/C/test/coroutine$
2樓 巨大八爪鱼 2026-6-5 17:14
My tutorial and take on C++20 coroutines

David Mazières

February, 2021

https://www.scs.stanford.edu/~dm/blog/c++-coroutines.html


std::coroutine_handle, std::noop_coroutine_handle

https://en.cppreference.com/cpp/coroutine/coroutine_handle

3樓 巨大八爪鱼 2026-6-5 17:34

【test.cpp】

#include <coroutine>

#include <iostream>


struct return_object {

    struct promise_type {

        return_object get_return_object() {

            std::cout << "get_return_object" << std::endl;

            return_object obj;

            obj.that = this;

            return obj;

        }

        std::suspend_never initial_suspend() {

            std::cout << "initial_suspend" << std::endl;

            std::suspend_never never;

            return never;

        }

        std::suspend_never final_suspend() noexcept {

            std::cout << "final_suspend" << std::endl;

            std::suspend_never never;

            return never;

        }

        void unhandled_exception() {

            std::cout << "unhandled_exception" << std::endl;

        }

    };


    promise_type *that;

};


return_object counter() {

    std::cout << "counter begin" << std::endl;

    for (int i = 0; i < 2; i++) {

        std::suspend_always a;

        co_await a;

        std::cout << "counter: " << i << std::endl;

    }

}


int main()

{

    return_object r = counter();

    std::coroutine_handle<return_object::promise_type> h = std::coroutine_handle<return_object::promise_type>::from_promise(*r.that);


    for (int i = 0; i < 2; i++) {

        std::cout << "in main function" << std::endl;

        h();

    }

    return 0;

}


【程序运行结果】

oct1158@oct1158-ubuntu:~/Documents/Code/C/test/coroutine$ ./test
get_return_object
initial_suspend
counter begin
in main function
counter: 0
in main function
counter: 1
final_suspend
oct1158@oct1158-ubuntu:~/Documents/Code/C/test/coroutine$


(因为counter函数执行完毕了,所以h自动销毁了,不用自己调用h.destroy())。

4樓 巨大八爪鱼 2026-6-5 17:48
Windows 7的Visual Studio 2019支持ISO C++20 标准 (/std:c++20),所以可以使用协程。

但是因为无法使用Visual Studio 2017 - Windows XP (v141_xp)编译器,所以编译出来的程序不支持Windows XP系统。

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張