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