libcopp  2.2.0
sample_readme_4.cpp
Go to the documentation of this file.
1 #include <inttypes.h>
2 #include <stdint.h>
3 #include <cstdio>
4 #include <cstring>
5 #include <ctime>
6 #include <iostream>
7 
8 // include context header file
10 #include <libcotask/task.h>
11 
12 // define the stack pool type
13 typedef copp::stack_pool<copp::allocator::default_statck_allocator> stack_pool_t;
14 
15 // define how to create coroutine context
17  using stack_allocator_type = copp::allocator::stack_allocator_pool<stack_pool_t>;
18  using coroutine_type = copp::coroutine_context_container<stack_allocator_type>;
19  using value_type = int;
20 };
21 
22 // create a stack pool
23 static stack_pool_t::ptr_t global_stack_pool = stack_pool_t::create();
24 
25 typedef cotask::task<sample_macro_coroutine> sample_task_t;
26 
27 int main() {
28 #if defined(LIBCOTASK_MACRO_ENABLED)
29 
30  global_stack_pool->set_min_stack_number(4);
31  std::cout << "stack pool=> used stack number: " << global_stack_pool->get_limit().used_stack_number
32  << ", used stack size: " << global_stack_pool->get_limit().used_stack_size
33  << ", free stack number: " << global_stack_pool->get_limit().free_stack_number
34  << ", free stack size: " << global_stack_pool->get_limit().free_stack_size << std::endl;
35  // create two coroutine task
36  {
37  copp::allocator::stack_allocator_pool<stack_pool_t> alloc(global_stack_pool);
38  sample_task_t::ptr_t co_task = sample_task_t::create(
39  []() {
40  std::cout << "task " << cotask::this_task::get<sample_task_t>()->get_id() << " started" << std::endl;
42  std::cout << "task " << cotask::this_task::get<sample_task_t>()->get_id() << " resumed" << std::endl;
43  return 0;
44  },
45  alloc);
46 
47  if (!co_task) {
48  std::cerr << "create coroutine task with stack pool failed" << std::endl;
49  return 0;
50  }
51 
52  std::cout << "stack pool=> used stack number: " << global_stack_pool->get_limit().used_stack_number
53  << ", used stack size: " << global_stack_pool->get_limit().used_stack_size
54  << ", free stack number: " << global_stack_pool->get_limit().free_stack_number
55  << ", free stack size: " << global_stack_pool->get_limit().free_stack_size << std::endl;
56 
57  // ..., then do anything you want to do with these tasks
58  }
59 
60  std::cout << "stack pool=> used stack number: " << global_stack_pool->get_limit().used_stack_number
61  << ", used stack size: " << global_stack_pool->get_limit().used_stack_size
62  << ", free stack number: " << global_stack_pool->get_limit().free_stack_number
63  << ", free stack size: " << global_stack_pool->get_limit().free_stack_size << std::endl;
64 
65  {
66  copp::allocator::stack_allocator_pool<stack_pool_t> alloc(global_stack_pool);
67  sample_task_t::ptr_t co_another_task = sample_task_t::create(
68  []() {
69  std::cout << "task " << cotask::this_task::get<sample_task_t>()->get_id() << " started" << std::endl;
71  std::cout << "task " << cotask::this_task::get<sample_task_t>()->get_id() << " resumed" << std::endl;
72  return 0;
73  },
74  alloc);
75 
76  if (!co_another_task) {
77  std::cerr << "create coroutine task with stack pool failed" << std::endl;
78  return 0;
79  }
80 
81  // ..., then do anything you want to do with these tasks
82  }
83 
84  std::cout << "stack pool=> used stack number: " << global_stack_pool->get_limit().used_stack_number
85  << ", used stack size: " << global_stack_pool->get_limit().used_stack_size
86  << ", free stack number: " << global_stack_pool->get_limit().free_stack_number
87  << ", free stack size: " << global_stack_pool->get_limit().free_stack_size << std::endl;
88 #else
89  std::cerr << "this sample require cotask enabled." << std::endl;
90 #endif
91  return 0;
92 }
virtual int yield(void **priv_data)=0
LIBCOPP_COTASK_API impl::task_impl * get_task() LIBCOPP_MACRO_NOEXCEPT
get current running task
Definition: this_task.cpp:9
static stack_pool_t::ptr_t global_stack_pool
copp::stack_pool< copp::allocator::default_statck_allocator > stack_pool_t
cotask::task< sample_macro_coroutine > sample_task_t
int main()
copp::allocator::stack_allocator_pool< stack_pool_t > stack_allocator_type
copp::coroutine_context_container< stack_allocator_type > coroutine_type