Skip to content

An asynchronous high concurrency library on Linux based on C++20 concurrency and io_uring

License

Notifications You must be signed in to change notification settings

AomaYple/coContext

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

介绍

本项目是Linux上一个基于C++20 协程io_uring的异步高并发库,让你可以以同步的方式实现异步逻辑,轻松实现高性能的高并发程序

特性

  • 绝大部分系统调用,如read write send recv
  • 纳秒级别的定时器sleep(1s, 1ns)
  • IO超时recv(socketFileDescriptor, buffer, 0) | timeout(1s)
  • IO取消cancel(taskId) cancel(fileDescriptor) cancelAny()
  • 嵌套任意数量任意返回值的协程
  • 多线程
  • 异步高性能多级别的日志系统
  • 直接文件描述符,可以与普通文件描述符相互转换
  • 多发射IO
  • 零拷贝发送
  • 百万级并发

基础用法

仅需引入coContext.hpp头文件,即可使用所有功能

#include <coContext/coContext.hpp> 

简单示例,向调度器添加function协程,启动调度器;function协程发起noOperation操作

#include <coContext/coContext.hpp>

[[nodiscard]] auto function() -> coContext::Task<> {    // Task模板参数为<>,表示该协程不返回任何值
    co_await coContext::noOperation();    // 发起noOperation操作
    // co_return; 无返回值的协程可以省略co_return语句
}

auto main() -> int {
    spawn(function);    // 将function函数作为协程任务加入到协程调度器中

    coContext::run();    // 启动调度器
}

依赖

建议使用Arch Linux

构建

cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release --install-prefix=your_absolute_path
cd build
ninja

额外CMake选项

  • -DNATIVE=ON 启用本机指令集(只在Release下生效)
  • -DEXAMPLE=ON 启用示例
  • -DBENCHMARK=ON 启用性能测试

安装

ninja install

导入

find_package(coContext REQUIRED)
target_link_libraries(your_target
        PRIVATE
        coContext
)

注意事项

  • 用户只能使用coContextcoContext::logger命名空间下的函数和类
  • coContext::Tast<T>只能用于声明协程函数的返回值

性能

环境:

  • 16 × 11th Gen Intel® Core™ i7-11800H @ 2.30GHz
  • 2 × 8GB DDR4 3200MHz
  • Arch Linux 6.13.1-zen1-1-zen (64 位)
  • gcc (GCC) 14.2.1 20240910
  • liburing 2.9
  • mimalloc 2.1.9

测试:
使用 wrk进行性能测试

  • coContext benchmark/coContext.cpp
    ❯ wrk -t $(nproc) -c 1007 http://localhost:8080
    Running 10s test @ http://localhost:8080
      16 threads and 1007 connections
      Thread Stats   Avg      Stdev     Max   +/- Stdev
        Latency   571.64us    3.43ms 233.47ms   98.17%
        Req/Sec   104.55k    43.06k  196.86k    67.78%
      16600241 requests in 10.10s, 601.59MB read
    Requests/sec: 1644235.82
    Transfer/sec:     59.59MB
    

About

An asynchronous high concurrency library on Linux based on C++20 concurrency and io_uring

Resources

License

Stars

Watchers

Forks