libcopp  1.2.1
sample_readme_1.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstring>
3 #include <inttypes.h>
4 #include <iostream>
5 #include <stdint.h>
6 
7 // include context header file
9 
10 // define a coroutine runner
11 int my_runner(void *) {
13 
14  std::cout << "cortoutine " << addr << " is running." << std::endl;
15 
16  addr->yield();
17 
18  std::cout << "cortoutine " << addr << " is resumed." << std::endl;
19 
20  return 1;
21 }
22 
23 int main() {
24  typedef copp::coroutine_context_default coroutine_t;
25 
26  // create a coroutine
27  copp::coroutine_context_default::ptr_t co_obj = coroutine_t::create(my_runner);
28  std::cout << "cortoutine " << co_obj << " is created." << std::endl;
29 
30  // start a coroutine
31  co_obj->start();
32 
33  // yield from my_runner
34  std::cout << "cortoutine " << co_obj << " is yield." << std::endl;
35  co_obj->resume();
36 
37  std::cout << "cortoutine " << co_obj << " exit and return " << co_obj->get_ret_code() << "." << std::endl;
38  return 0;
39 }
int main()
int my_runner(void *)
coroutine_context * get_coroutine() UTIL_CONFIG_NOEXCEPT
get current coroutine
int yield(void **priv_data=UTIL_CONFIG_NULLPTR)
yield coroutine
coroutine container contain stack context, stack allocator and runtime fcontext
base type of all coroutine context