add new example of chained coroutines

This commit is contained in:
Oliver Kowalke 2015-09-18 21:23:47 +02:00
parent cd2c00c8c6
commit 7183bb6e4c
2 changed files with 74 additions and 0 deletions

View File

@ -47,3 +47,7 @@ exe parser
exe segmented
: segmented.cpp
;
exe chained
: chained.cpp
;

70
example/chained.cpp Normal file
View File

@ -0,0 +1,70 @@
// Copyright Oliver Kowalke 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cstdlib>
#include <iostream>
#include <boost/coroutine2/all.hpp>
template<typename coroutine>
struct test
{
using pull_type = typename coroutine::pull_type;
using push_type = typename coroutine::push_type;
pull_type * child = nullptr;
void start_child_coroutine()
{
child = new pull_type
(
[](push_type & yield)
{
std::cout << "2";
yield();
std::cout << "2";
yield();
std::cout << "2";
yield();
std::cout << "2";
yield();
std::cout << "2";
yield();
std::cout << "2";
}
);
}
pull_type start_parent_coroutine()
{
return pull_type
(
[this](push_type & yield)
{
std::cout << "1";
start_child_coroutine();
yield();
std::cout << "1";
}
);
}
test()
{
auto parent = start_parent_coroutine();
while (*child)
{
(*child)();
}
std::cout << std::endl;
}
};
int main() {
test<boost::coroutines2::coroutine<void>> t2;
std::cout << "Done" << std::endl;
}