Skip to content

C++20 Coroutine Library for Education Purpose (WIP)

Notifications You must be signed in to change notification settings

anthosq/co_async

 
 

Repository files navigation

co_async

基于 C++20 协程的高并发异步 I/O 库(小彭老师教学用)

教学视频

steps 目录 是本库代码逐渐成形的过程,可以配合教学视频自己动手尝试。

使用案例

import co_async;
import std;

using namespace co_async;

Task<> amain() {
    auto serv = co_await server_bind({"127.0.0.1", 8080});
    HTTPServer http;
    http.route("/", [] (HTTPRequest const &request) -> Task<HTTPResponse> {
        if (request.method != "GET")
            co_return HTTPServer::make_error_response(405);
        co_return {
            .status = 200,
            .headers = {
                {"content-type", "text/html;charset=utf-8"},
            },
            .body = "<h1>It works!</h1>",
        };
    });

    co_await stdio().putline("正在监听: " + serv.address().toString());
    while (1) {
        auto conn = co_await server_accept(serv);
        co_await stdio().putline("收到请求: " + serv.address().toString());
        co_spawn(http.process_connection(FileStream(std::move(conn))));
    }
}

int main() {
    co_spawn_and_wait(amain());
    return 0;
}

examples 目录 中有更多案例。

平台要求

  • Linux 内核 >= 5.1
  • GCC >= 10
  • Clang >= 16

小彭老师推荐使用 Arch Linux 系统作为开发平台

或者:

  • Windows >= 10(施工中,稍后支持)
  • Visual Studio >= 2022(施工中,稍后支持)

安装与导入

作为单个头文件导入

点击此处 下载 co_async.hpp,然后在你的项目中引入即可:

#include "co_async.hpp"

作为 C++20 模块导入

将本项目下载到你的项目中作为子文件夹,引入并链接:

add_subdirectory(co_async)
target_link_libraries(你的名字 PRIVATE co_async)

在你的代码中 import 即可:

import co_async;

需要 GCC >= 11、Clang >= 17、MSVC >= 19 以支持 C++20 模块

About

C++20 Coroutine Library for Education Purpose (WIP)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 96.5%
  • Python 2.6%
  • CMake 0.9%