libcopp  2.2.0
sample_readme_2.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 // include task header file
4 #include <libcotask/task.h>
5 
6 typedef cotask::task<> my_task_t;
7 
8 int main() {
9  // create a task using factory function [with lambda expression]
10  my_task_t::ptr_t task = my_task_t::create([]() {
11  std::cout << "task " << cotask::this_task::get<my_task_t>()->get_id() << " started" << std::endl;
13  std::cout << "task " << cotask::this_task::get<my_task_t>()->get_id() << " resumed" << std::endl;
14  return 0;
15  });
16 
17  std::cout << "task " << task->get_id() << " created" << std::endl;
18  // start a task
19  task->start();
20 
21  std::cout << "task " << task->get_id() << " yield" << std::endl;
22  task->resume();
23  std::cout << "task " << task->get_id() << " stoped, ready to be destroyed." << std::endl;
24  return 0;
25 }
UTIL_FORCEINLINE id_type get_id() const LIBCOPP_MACRO_NOEXCEPT
Definition: task_impl.h:74
virtual int yield(void **priv_data)=0
Definition: task.h:37
int start(void *priv_data, EN_TASK_STATUS expected_status=EN_TS_CREATED) override
Definition: task.h:462
int resume(void *priv_data, EN_TASK_STATUS expected_status=EN_TS_WAITING) override
Definition: task.h:543
LIBCOPP_COTASK_API impl::task_impl * get_task() LIBCOPP_MACRO_NOEXCEPT
get current running task
Definition: this_task.cpp:9
cotask::task my_task_t
int main()