【发布时间】:2013-12-20 20:31:44
【问题描述】:
当前的 Boost 1.55 实现提供了两种unidirectional coroutines。一种是拉式,是一种不带参数,返回值给主上下文的协程;另一种是push-type,它是一个从主上下文接受参数但不返回值的协程。
如何将这两者结合起来创建一个既接受参数又返回值的双向协程?从表面上看,这似乎应该是可能的,但我不太清楚如何使用我在boost::coroutine 中的构建块来做到这一点。在旧的 Boost 中曾经有一个双向协程,但它现在已被弃用且未记录在案,所以我不应该依赖它。
即,我想要类似的东西:
void accumulate( pull_func &in, push_func &out )
{
int x = 0;
while ( in )
{
x += in.get() ; // transfers control from main context
out(x); // yields control to main context
}
}
void caller( int n )
{
bidirectional_coro( accumulate );
for ( int i = 0 ; i < n ; ++i )
{
int y = accumulate(i);
printf( "%d ", y ); // "0 1 3 6 10" etc
}
}
【问题讨论】: