C++ 是否可以使用Boost.Coroutine嵌套协同路由?
当我已经在一个协同程序中时,我想呼叫一个协同程序。是否可以使用?是的,它很容易:C++ 是否可以使用Boost.Coroutine嵌套协同路由?,c++,boost,c++11,coroutine,C++,Boost,C++11,Coroutine,当我已经在一个协同程序中时,我想呼叫一个协同程序。是否可以使用?是的,它很容易: #include <iostream> #include <boost/coroutine/coroutine.hpp> typedef boost::coroutines::coroutine<int()> generator; void bar(generator::caller_type& yield) { for (std::size_t i = 100;
#include <iostream>
#include <boost/coroutine/coroutine.hpp>
typedef boost::coroutines::coroutine<int()> generator;
void bar(generator::caller_type& yield)
{
for (std::size_t i = 100; i < 110; ++i)
yield(i);
}
void foo(generator::caller_type& yield)
{
for (std::size_t i = 0; i < 10; ++i)
{
generator nested_gen(bar);
while (nested_gen)
{
std::cout << "foo: " << nested_gen.get() << std::endl;
nested_gen();
}
yield(i);
}
}
int main()
{
generator gen(foo);
while (gen)
{
std::cout << "main: " << gen.get() << std::endl;
gen();
}
return 0;
};
#包括
#包括
typedef boost::coroutines::coroutinegenerator;
空栏(生成器::调用方类型和产量)
{
对于(标准:尺寸i=100;i<110;++i)
产量(i);
}
void foo(生成器::调用方类型和产量)
{
对于(标准::尺寸i=0;i<10;++i)
{
发电机(巴);
while(嵌套的_gen)
{
标准::cout
#include <iostream>
#include <boost/coroutine/asymmetric_coroutine.hpp>
using generator = typename boost::coroutines::asymmetric_coroutine<std::size_t>::pull_type;
using yield_type = typename boost::coroutines::asymmetric_coroutine<std::size_t>::push_type;
void bar(yield_type& yield)
{
for (std::size_t i = 100; i < 110; ++i)
yield(i);
}
void foo(yield_type& yield)
{
for (std::size_t i = 0; i < 10; ++i)
{
generator nested_gen{bar};
while (nested_gen)
{
std::cout << "foo: " << nested_gen.get() << '\n';
nested_gen();
}
yield(i);
}
}
int main()
{
generator gen{foo};
while (gen)
{
std::cout << "main: " << gen.get() << '\n';
gen();
}
return 0;
};