boost corouting2

协程的一个用途是生成器。生成器是一种通用的设计模式,能简洁清晰的表达数据生产和消费关系。

#include <iostream>
#include <boost/coroutine2/coroutine.hpp>
using namespace std;
 
typedef boost::coroutines2::coroutine<int> coro_t;

void source(coro_t::push_type& sink)
{
    for (int i=65;i<91;++i){
        std::cout << "value " << i << " denote ";
        sink(i);  //suspend执行流,并且给caller传递值
    }
}

int main()
{
    coro_t::pull_type generator( source );
 
    for(auto i : generator)
        std::cout << char(i) << endl;
}

为了达到类似效果,可以自定义一个class支持begin(),end()等基础设施,以便符合基于range for的语法糖。或者把生成的数据一次放入一个容器,当数据量特别大时,面临空间复杂性(吃内存)的技术困扰。协程generator方案是最优秀的。

corouting2协程可以反着做成消费者

#include <iostream>
#include <boost/coroutine2/coroutine.hpp>
using namespace std;
typedef boost::coroutines2::coroutine<int> coro_t;

int main()
{
    //处理事件的协程,它作为消费者角色。
    coro_t::push_type consumer(
        [&](coro_t::pull_type& source) {  //协程的入口函数体,lambda形式
        int n = 0;
        while (source) {
            cout << "entry times "<< ++n <<". get " << source.get() << std::endl;
            source();  //suspend执行,等待下一个事件(调用)
        }
    });

    int i = 0;
    std::cout << "please input a number:" << std::endl;
    while (std::cin >> i) {
        consumer(i); //切换到consumer这个协程执行
        std::cout << "please input a number:" << std::endl;
    }

}

迭代器写法:

#include <iostream>
#include <boost/coroutine2/coroutine.hpp>
using namespace std;
 
typedef boost::coroutines2::coroutine<int> coro_t;

    void foo(coro_t::pull_type & source){
        using coIter = boost::coroutines2::coroutine<int>::pull_type::iterator;
        for (coIter start = begin(source); start != end(source); ++start) {
            std::cout << "retrieve "<<*start << "\n";
        }
    }

int main()
{
    coro_t::push_type sink(foo);
    std::cout << "sink(1)" << "\n";
    sink(1);
    std::cout << "sink(2)" << "\n";
    sink(2);
    std::cout << "ok" << "\n";
    
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值