我使用 boost 1.53 的协程并尝试来自http://www.boost.org/doc/libs/1_53_0/libs/coroutine/doc/html/coroutine/coroutine.html#coroutine.coroutine.calling_a_coroutine的代码:
typedef boost::coroutines::coroutine< void() > coro_t;
void fn( coro_t::caller_type & ca, int j) {
for(int i = 0; i < j; ++i) {
std::cout << "fn(): local variable i == " << i << std::endl;
ca();
}
}
int main(int argc, char *argv[]) {
// bind parameter '7' to coroutine-fn
coro_t c( boost::bind( fn, _1, 7) );
std::cout << "main() starts coroutine c" << std::endl;
while ( c)
{
std::cout << "main() calls coroutine c" << std::endl;
// execution control is transferred to c
c();
}
std::cout << "Done" << std::endl;
return EXIT_SUCCESS;
}
输出:
fn(): local variable i == 0
main() starts coroutine c
main() calls coroutine c
fn(): local variable i == 1
main() calls coroutine c
fn(): local variable i == 2
main() calls coroutine c
fn(): local variable i == 3
main() calls coroutine c
fn(): local variable i == 4
main() calls coroutine c
fn(): local variable i == 5
main() calls coroutine c
fn(): local variable i == 6
main() calls coroutine c
Done
输出与前两个中的链接不同。它是一个错误吗?