Coder Social home page Coder Social logo

cocpp's Introduction

README

简介

cocpp是一个基于现代C++的协程库(c++20),其主要特点是易于使用,会用std::thread,就会用cocpp,封装性好,学习成本低。

目前的环境要求为:

  • x86_64体系架构
  • linux操作系统
  • gcc支持C++20

hello world

一个最简单的Demo:

#include "cocpp/cocpp.h"

#include <cstdio>
#include <string>

int main()
{
    cocpp::co c1([]() {
        return std::string("Hello ");
    });

    auto ret = c1.wait<std::string>();
    printf("%s", (ret + "World!\n").c_str());
}

特性介绍

目前cocpp支持的特性说明如下:

  • 自由的协程入口函数

    任何样式的函数都可以作为函数签名,无论是有返回值的、无返回值的、无参数的、一个参数的、多个参数,都可以作为协程的入口函数。

  • 协程优先级

    协程创建时可以指定协程的优先级,也可以在协程运行过程中修改优先级(优先级0~99,其中0最高,99最低,默认创建的协程优先级为99)。不同优先级间以高优先级协程优先调度,相同优先级的协程轮询调度。

  • 多线程负载均衡

    协程可以在不同的线程间迁移实现以负载均衡。

  • 死循环强制调度

    当协程环境超过设置的阈值没有调度时,会从外部强制调度。

  • 系统调用超时,重新分配协程

    当一个协程由于系统调用而被阻塞时,管理器会将当前线程上其余所有可迁移的协程迁移到其他合适的执行环境中。

  • 协程偷取

    当当前线程空闲了,会尝试从其他执行环境偷取协程来执行。

  • 协程同步工具

    具有丰富的协程同步工具。信号量、二值信号量、条件变量、互斥锁、递归锁、读写锁等。

  • 用于通信的channel

    和Go一样,具有用于协程通信的channel实现。

  • 共享栈

    多个协程可以共用一个栈空间,以此减少内存使用(共享栈协程不支持迁移)。

  • 运行环境绑定

    协程可以与某个运行环境绑定,禁止迁移,这在某些场景下很有用,如使用线程局部存储的场景。

  • 返回值获取

    协程的返回值获取非常简单易用。

  • 协程局部存储

    方便的协程局部存储,让每个协程拥有自己的私有数据。

  • 支持 pipeline 的并行数据处理模式

#include "cocpp/cocpp.h"
#include <iostream>

using namespace cocpp;
using namespace std;

int main()
{
    int source = 0;
    auto ch = co_pipeline<int>([&source]() -> std::optional<int> { // 生成[0, 1000000) 序列
                  if (source < 1000000)
                  {
                      return source++;
                  }
                  return std::nullopt;
              })
              | pipeline::take(2000) // 取前 2000 项
              | pipeline::skip(1000) // 跳过前 1000 项 (相当于取[1000, 1999))
              | pipeline::fork(10, [](int n) -> int { // 每个数字乘以2 ,10个协程同时计算
                    return n * 2;
                })
              | pipeline::filter([](int n) { return n % 3 == 0; })  // 过滤出 3 的倍数
              | pipeline::reduce([](int n, int m) { return n + m; }, 0) // 求和
              | pipeline::chan(); // 转换为 channel

    for (auto t : ch)
    {
        cout << t << endl;
    }
}

编译安装

安装xmake

此工程使用xmake管理,所以构建需要安装xmake,安装方法:

  • 通过curl
bash <(curl -fsSL https://xmake.io/shget.text)
  • 通过wget
bash <(wget https://xmake.io/shget.text -O -)

安装cmake

如果需要编译运行测试用例,需要依赖cmake。

拉取代码

git clone https://github.com/skyfireitdiy/cocpp

如果需要编译运行测试程序,需要将gtest和mockcpp也clone下来,这两个工程作为cocpp的子模块,可以直接使用如下命令:

git clone --recursive https://github.com/skyfireitdiy/cocpp

或者如果已经clone过了,可以在源码目录下使用如下命令更新子模块:

git submodule init
git submodule update

编译

cocpp

在源码目录,使用以下命令编译cocpp:

xmake f --mode=release # 设置编译模式为release,也可以设置为debug
xmake b cocpp # 编译cocpp

test

如果需要运行测试用例,需要先编译gtest与mockcpp:

./build_3rd.sh

然后编译运行test:

xmake f --mode=release # 设置编译模式为release,也可以设置为debug
xmake b test # 编译并运行cocpp

安装

使用如下命令可以安装至/usr/local/目录(需要root权限):

xmake install cocpp

或者也可以指定安装目录:

xmake install -o /install/path cocpp

使用示例

使用的例子参见example目录。

联系作者

cocpp's People

Contributors

skyfireitdiy avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.