libcopp  1.1.0
sample_readme_3.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
9 #include <libcotask/task.h>
10 #include <libcotask/task_manager.h>
11 
12 // create a task manager
17 
18 // If you task manager to manage timeout, it's important to call tick interval
19 
20 void tick() {
21  // the first parameter is second, and the second is nanosecond
22  task_mgr->tick(time(NULL), 0);
23 }
24 
25 int main() {
26 #if defined(UTIL_CONFIG_COMPILER_CXX_LAMBDAS) && UTIL_CONFIG_COMPILER_CXX_LAMBDAS
27  // create two coroutine task
28  task_ptr_type co_task = my_task_t::create([]() {
29  std::cout << "task " << cotask::this_task::get<my_task_t>()->get_id() << " started" << std::endl;
31  std::cout << "task " << cotask::this_task::get<my_task_t>()->get_id() << " resumed" << std::endl;
32  return 0;
33  });
34  task_ptr_type co_another_task = my_task_t::create([]() {
35  std::cout << "task " << cotask::this_task::get<my_task_t>()->get_id() << " started" << std::endl;
37  std::cout << "task " << cotask::this_task::get<my_task_t>()->get_id() << " resumed" << std::endl;
38  return 0;
39  });
40 
41 
42  int res = task_mgr->add_task(co_task, 5, 0); // add task and setup 5s for timeout
43  if (res < 0) {
44  std::cerr << "some error: " << res << std::endl;
45  return res;
46  }
47 
48  res = task_mgr->add_task(co_another_task); // add task without timeout
49  if (res < 0) {
50  std::cerr << "some error: " << res << std::endl;
51  return res;
52  }
53 
54  res = task_mgr->start(co_task->get_id());
55  if (res < 0) {
56  std::cerr << "start task " << co_task->get_id() << " failed, error code: " << res << std::endl;
57  }
58 
59  res = task_mgr->start(co_another_task->get_id());
60  if (res < 0) {
61  std::cerr << "start task " << co_another_task->get_id() << " failed, error code: " << res << std::endl;
62  }
63 
64  res = task_mgr->resume(co_task->get_id());
65  if (res < 0) {
66  std::cerr << "resume task " << co_task->get_id() << " failed, error code: " << res << std::endl;
67  }
68 
69  res = task_mgr->kill(co_another_task->get_id());
70  if (res < 0) {
71  std::cerr << "kill task " << co_another_task->get_id() << " failed, error code: " << res << std::endl;
72  } else {
73  std::cout << "kill task " << co_another_task->get_id() << " finished." << std::endl;
74  }
75 
76 #else
77  std::cerr << "lambda not supported, this sample is not available." << std::endl;
78 #endif
79  return 0;
80 }
cotask::task my_task_t
void tick()
cotask::task_manager< my_task_t > mgr_t
std::shared_ptr< self_t > ptr_t
Definition: task_manager.h:63
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
task manager
Definition: task_manager.h:56
impl::task_impl * get_task() UTIL_CONFIG_NOEXCEPT
get current running task
Definition: this_task.cpp:15
int main()
static ptr_t create()
create a new task manager
Definition: task_manager.h:139
virtual int yield(void **priv_data)=0
my_task_t::ptr_t task_ptr_type
mgr_t::ptr_t task_mgr