libcopp
2.3.1
Loading...
Searching...
No Matches
sample
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
#include <inttypes.h>
11
#include <stdint.h>
12
#include <cstdio>
13
#include <cstring>
14
#include <iostream>
15
16
// include manager header file
17
#include <
libcopp/coroutine/coroutine_context_container.h
>
18
19
// define a coroutine runner
20
class
my_runner
{
21
public
:
22
int
operator()
(
void
*) {
23
// ... your code here ...printf("cortoutine %" PRIxPTR " exit and return %d.\n", (intptr_t)&co_obj,
24
// 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
37
copp::coroutine_context_default::ptr_t
co_obj =
copp::coroutine_context_default::create
(
my_runner
());
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
}
coroutine_context_container::create
static ptr_type create(callback_type &&runner, allocator_type &alloc, size_t stack_sz=0, size_t private_buffer_size=0, size_t coroutine_size=0) LIBCOPP_MACRO_NOEXCEPT
create and init coroutine with specify runner and specify stack size
Definition
coroutine_context_container.h:71
coroutine_context_container::ptr_t
ptr_type ptr_t
Definition
coroutine_context_container.h:36
my_runner
Definition
sample_default_coroutine_context.cpp:20
my_runner::operator()
int operator()(void *)
Definition
sample_default_coroutine_context.cpp:22
coroutine_context_container.h
main
int main()
Definition
sample_default_coroutine_context.cpp:35
Generated by
1.9.8