Ticket #13064: main_coro.cpp

File main_coro.cpp, 932 bytes (added by anonymous, 5 years ago)

coroutine version

Line 
1#include <boost/coroutine/all.hpp>
2#include <boost/coroutine2/all.hpp>
3#include <boost/variant.hpp>
4#include <boost/bind.hpp>
5#include <iostream>
6
7using Coro = boost::coroutines::coroutine<void>;
8
9class TestRAII
10{
11public:
12 TestRAII (void) {std::cout << "TestRAII::ctor" << std::endl;}
13 ~TestRAII (void) {std::cout << "TestRAII::dtor" << std::endl;}
14};
15
16class worker
17{
18 public:
19 void fn(Coro::pull_type &aSink)
20 {
21 TestRAII testRaii;
22
23 std::cout << "coro call 1" << std::endl;
24 aSink();
25 std::cout << "coro call 2" << std::endl;
26 aSink();
27 std::cout << "coro call 3" << std::endl;
28 }
29};
30
31int main(int /*argc*/, char** /*argv*/)
32{
33 worker w;
34
35 auto* coro = new Coro::push_type(boost::bind(&worker::fn, &w, _1));
36
37 std::cout << "main: before coro call..." << std::endl;
38 (*coro)();
39 std::cout << "main: after coro call..." << std::endl;
40 delete coro;
41 std::cout << "main: after coro delete..." << std::endl;
42
43 return 0;
44}
45