21 #ifdef LIBCOTASK_MACRO_ENABLED
23 # if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
25 # define CALC_CLOCK_T std::chrono::system_clock::time_point
26 # define CALC_CLOCK_NOW() std::chrono::system_clock::now()
27 # define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
28 # define CALC_NS_AVG_CLOCK(x, y) \
29 static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
31 # define CALC_CLOCK_T clock_t
32 # define CALC_CLOCK_NOW() clock()
33 # define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
34 # define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
38 int max_task_number = 100000;
39 size_t g_stack_size = 16 * 1024;
41 struct my_macro_coroutine {
42 using stack_allocator_type = copp::allocator::stack_allocator_malloc;
43 using coroutine_type = copp::coroutine_context_container<stack_allocator_type>;
47 using my_task_t = cotask::task<my_macro_coroutine>;
49 std::vector<my_task_t::ptr_t> task_arr;
52 int my_task_action(
void *) {
63 int main(
int argc,
char *argv[]) {
64 puts(
"###################### task (stack using malloc/free) ###################");
65 printf(
"########## Cmd:");
66 for (
int i = 0; i < argc; ++i) {
67 printf(
" %s", argv[i]);
72 max_task_number = atoi(argv[1]);
80 g_stack_size = atoi(argv[3]) * 1024;
82 if (g_stack_size < copp::stack_traits::minimum_size()) {
83 g_stack_size = copp::stack_traits::minimum_size();
86 time_t begin_time = time(
nullptr);
90 task_arr.reserve(
static_cast<size_t>(max_task_number));
91 while (task_arr.size() <
static_cast<size_t>(max_task_number)) {
92 task_arr.push_back(my_task_t::create(my_task_action, g_stack_size));
95 time_t end_time = time(
nullptr);
97 printf(
"create %d task, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
98 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
101 begin_time = end_time;
102 begin_clock = end_clock;
105 for (
int i = 0; i < max_task_number; ++i) {
106 task_arr[i]->start();
110 bool continue_flag =
true;
111 long long real_switch_times =
static_cast<long long>(0);
113 while (continue_flag) {
114 continue_flag =
false;
115 for (
int i = 0; i < max_task_number; ++i) {
116 if (
false == task_arr[i]->is_completed()) {
117 continue_flag =
true;
119 task_arr[i]->resume();
124 end_time = time(
nullptr);
126 printf(
"switch %d tasks %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
127 real_switch_times,
static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
130 begin_time = end_time;
131 begin_clock = end_clock;
135 end_time = time(
nullptr);
137 printf(
"remove %d tasks, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
138 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
145 puts(
"cotask disabled.");
LIBCOPP_COTASK_API impl::task_impl * get_task() LIBCOPP_MACRO_NOEXCEPT
get current running task
std::shared_ptr< cli::cmd_option_value > value_type
#define CALC_NS_AVG_CLOCK(x, y)