协程的一个用途是生成器。生成器是一种通用的设计模式,能简洁清晰的表达数据生产和消费关系。
#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";
}