libcopp  1.1.0
sample_readme_4.cpp
Go to the documentation of this file.
1 #include <cstdio>
2 #include <cstring>
3 #include <ctime>
4 #include <inttypes.h>
5 #include <iostream>
6 #include <stdint.h>
7 
8 // include context header file
10 #include <libcotask/task.h>
11 
12 // define the stack pool type
14 
15 // define how to create coroutine context
19 };
20 
21 // create a stack pool
23 
25 
26 int main() {
27 #if defined(LIBCOTASK_MACRO_ENABLED) && defined(UTIL_CONFIG_COMPILER_CXX_LAMBDAS) && UTIL_CONFIG_COMPILER_CXX_LAMBDAS
28 
29  global_stack_pool->set_min_stack_number(4);
30  std::cout << "stack pool=> used stack number: " << global_stack_pool->get_limit().used_stack_number
31  << ", used stack size: " << global_stack_pool->get_limit().used_stack_size
32  << ", free stack number: " << global_stack_pool->get_limit().free_stack_number
33  << ", free stack size: " << global_stack_pool->get_limit().free_stack_size << std::endl;
34  // create two coroutine task
35  {
38  []() {
39  std::cout << "task " << cotask::this_task::get<sample_task_t>()->get_id() << " started" << std::endl;
41  std::cout << "task " << cotask::this_task::get<sample_task_t>()->get_id() << " resumed" << std::endl;
42  return 0;
43  },
44  alloc);
45 
46  if (!co_task) {
47  std::cerr << "create coroutine task with stack pool failed" << std::endl;
48  return 0;
49  }
50 
51  std::cout << "stack pool=> used stack number: " << global_stack_pool->get_limit().used_stack_number
52  << ", used stack size: " << global_stack_pool->get_limit().used_stack_size
53  << ", free stack number: " << global_stack_pool->get_limit().free_stack_number
54  << ", free stack size: " << global_stack_pool->get_limit().free_stack_size << std::endl;
55 
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  {
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 << "lambda not supported, this sample is not available." << std::endl;
90 #endif
91  return 0;
92 }
copp::coroutine_context_container< stack_allocator_t > coroutine_t
static ptr_t create()
Definition: stack_pool.h:52
static stack_pool_t::ptr_t global_stack_pool
static ptr_t create(const Ty &functor, size_t stack_size=0, size_t private_buffer_size=0)
create task with functor
Definition: task.h:155
cotask::task< sample_macro_coroutine > sample_task_t
copp::allocator::stack_allocator_pool< stack_pool_t > stack_allocator_t
coroutine container contain stack context, stack allocator and runtime fcontext
impl::task_impl * get_task() UTIL_CONFIG_NOEXCEPT
get current running task
Definition: this_task.cpp:15
memory allocator this allocator will maintain buffer using malloc/free function
int main()
virtual int yield(void **priv_data)=0
copp::stack_pool< copp::allocator::default_statck_allocator > stack_pool_t
std::shared_ptr< stack_pool< TAlloc > > ptr_t
Definition: stack_pool.h:25