C++11 非对称协程库 stdex.coroutine

码农软件 · 软件分类 · C/C++开发工具 · 2019-11-09 20:43:03

软件介绍

C++11 非对称协程库(只需要单独一个.h文件)

支持几个必备的协程原语(命名空间coroutine):

  • 创建协程:routine_t  create( std::function<void()>  f );

  • 销毁协程:void  destroy( routine_t  id );

  • 恢复协程执行:int  resume( routine_t  id );

  • 放弃协程执行:void  yield();

  • 等待异步任务结果:TYPE  await(TYPE(*f)());

  • 获取当前协程的ID:routine_t current();

  • SPSC通信模板类:class Channel<T>:支持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;
}

本文地址:https://codercto.com/soft/d/18639.html

我是一只IT小小鸟

我是一只IT小小鸟

胡江堂、李成、唐雅薇、秦琴、蒋宇东、刘未鹏、居振梁、刘帅、温卫斌、张弦、张凯峰、庄表伟、宋劲杉、程露、黄小明、易晓东、简朝阳、林健、高昂、徐宥、辜新星 / 电子工业出版社 / 2009 / 29.80

一群IT小小鸟—— 来自十几所院校,或男生,或女生;或科班,或半路转行。 分布在不同的公司,或外企,或国企,或民企,老板有土有洋。 有失意,有快意;有泪水,有欢笑。在失望中追求希望,在迷茫中辨别方向。 他们用自己的成长故事,告诉在校的师弟师妹们: 青春太宝贵,千万别浪费;要想不浪费,万事早准备。一起来看看 《我是一只IT小小鸟》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器