libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
11int 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
23int 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}
int main()