libcopp  1.1.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 
11 #include <cstdio>
12 #include <cstring>
13 #include <inttypes.h>
14 #include <iostream>
15 #include <stdint.h>
16 
17 // include manager header file
19 
20 // define a coroutine runner
21 class my_runner {
22 public:
23  int operator()(void*) {
24  // ... your code here ...printf("cortoutine %" PRIxPTR " exit and return %d.\n", (intptr_t)&co_obj, 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
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 }
static ptr_t create(const callback_t &runner, allocator_type &alloc, size_t stack_sz=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
create and init coroutine with specify runner and specify stack size
int my_runner(void *)
int yield(void **priv_data=UTIL_CONFIG_NULLPTR)
yield coroutine
coroutine container contain stack context, stack allocator and runtime fcontext
element_type * get() const UTIL_CONFIG_NOEXCEPT