libcopp  2.2.0
sample_default_coroutine_context.cpp
Go to the documentation of this file.
1 /*
2  * sample_default_manager.cpp
3  *
4  * Created on: 2014年3月17日
5  * Author: owent
6  *
7  * Released under the MIT license
8  */
9 
10 #include <inttypes.h>
11 #include <stdint.h>
12 #include <cstdio>
13 #include <cstring>
14 #include <iostream>
15 
16 // include manager header file
18 
19 // define a coroutine runner
20 class my_runner {
21  public:
22  int operator()(void *) {
23  // ... your code here ...printf("cortoutine %" PRIxPTR " exit and return %d.\n", (intptr_t)&co_obj,
24  // co_obj.get_ret_code());
25  copp::coroutine_context_default *addr = copp::this_coroutine::get<copp::coroutine_context_default>();
26  std::cout << "cortoutine " << addr << " is running." << std::endl;
27 
28  addr->yield();
29  std::cout << "cortoutine " << addr << " is resumed." << std::endl;
30 
31  return 1;
32  }
33 };
34 
35 int main() {
36  // create a coroutine
37  copp::coroutine_context_default::ptr_t co_obj = copp::coroutine_context_default::create(my_runner());
38  std::cout << "cortoutine " << co_obj.get() << " is created." << std::endl;
39 
40  // start a coroutine
41  co_obj->start();
42 
43  // yield from runner
44  std::cout << "cortoutine " << co_obj.get() << " is yield." << std::endl;
45  co_obj->resume();
46 
47  std::cout << "cortoutine " << co_obj.get() << " exit and return " << co_obj->get_ret_code() << "." << std::endl;
48  return 0;
49 }
coroutine_context_container< allocator::default_statck_allocator > coroutine_context_default
int my_runner(void *)