libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
18
19// define a coroutine runner
20class 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
35int main() {
36 // create a coroutine
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}
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