【转】C++协程库coroutine使用

转自https://blog.csdn.net/hellufo2/article/details/53609487
https://my.oschina.net/attobit/blog/753962 项目源码
https://github.com/tonbit/coroutine

创建协程:routine_t create;
销毁协程:void destroy( routine_t id );
恢复协程:int resume( routine_t id );
等待异步任务结果:TYPE await(TYPE(*f)());
获取当前协程的ID:routine_t current();
SPSC通道模板类:class Channel:支持push()/pop()操作;

#include <iostream>
#include <chrono>
//只需下载include此文件
#include "coroutine.h"

//SPSC通道,多个生产者或消费者,协程调度行为不好控制
coroutine::Channel<int> channel; 

string async_func()
{
    std::this_thread::sleep_for(std::chrono::milliseconds(3000));
    return "21";
}

void routine_func1()
{
    //从通道中获取消息,如果没有消息会yield
    int i = channel.pop();
    std::cout << i << std::endl;

    i = channel.pop();
    std::cout << i << std::endl;
}

void routine_func2(int i)
{
    std::cout << "20" << std::endl;

    //放弃当前协程的执行,返回恢复点
    coroutine::yield();

    std::cout << "21" << std::endl;

    //异步执行任务,如果任务无法立即执行完毕,会yield
    string str = coroutine::await(async_func);
    std::cout << str << std::endl;
}

void thread_func()
{
    //创建协称,回调函数形式为:std::function<void()>
    coroutine::routine_t rt1 = coroutine::create(routine_func1);
    coroutine::routine_t rt2 = coroutine::create(std::bind(routine_func2, 2));

    std::cout << "00" << std::endl;

    //恢复rt1
    coroutine::resume(rt1);

    std::cout << "01" << std::endl;

    //恢复rt2
    coroutine::resume(rt2);

    std::cout << "02" << std::endl;
    //向通道推送消息
    channel.push(10);

    std::cout << "03" << std::endl;
    coroutine::resume(rt2);

    std::cout << "04" << std::endl;
    channel.push(11);

    std::cout << "05" << std::endl;

    //销毁协程。
    //建议:协程在执行完毕后统一释放,这样协程栈空间中的对象能够安全的被到释放。
    coroutine::destroy(rt1);
    coroutine::destroy(rt2);
}

int main()
{
    std::thread t1(thread_func);
    std::thread t2([](){
        //不支持跨线程的协程调度
    });
    t1.join();
    t2.join();
    return 0;
}

程序执行结果为:

00
01
20
02
10
03
21
04
11
05

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值