libcopp  2.2.0
sample_readme_1.cpp
Go to the documentation of this file.
1 #include <inttypes.h>
2 #include <stdint.h>
3 #include <cstdio>
4 #include <cstring>
5 #include <iostream>
6 
7 // include context header file
9 
10 // define a coroutine runner
11 int my_runner(void *) {
12  copp::coroutine_context *addr = copp::this_coroutine::get_coroutine();
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_type;
25 
26  // create a coroutine
27  copp::coroutine_context_default::ptr_t co_obj = coroutine_type::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 }
coroutine_context_container< allocator::default_statck_allocator > coroutine_context_default
LIBCOPP_COPP_API coroutine_context * get_coroutine() LIBCOPP_MACRO_NOEXCEPT
get current coroutine
int my_runner(void *)
int main()