libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
6typedef cotask::task<> my_task_t;
7
8int 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;
12 cotask::this_task::get_task()->yield();
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}
LIBCOPP_UTIL_FORCEINLINE id_type get_id() const LIBCOPP_MACRO_NOEXCEPT
Definition task_impl.h:76
Definition task.h:38
int start(void *priv_data, EN_TASK_STATUS expected_status=EN_TS_CREATED) override
Definition task.h:523
int resume(void *priv_data, EN_TASK_STATUS expected_status=EN_TS_WAITING) override
Definition task.h:604
cotask::task my_task_t
int main()