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 struct stack_mem_pool_t {
41 stack_mem_pool_t() : index(0), buff(nullptr) {}
47 int max_task_number = 100000;
48 size_t g_stack_size = 16 * 1024;
50 struct my_macro_coroutine {
51 using stack_allocator_type = copp::allocator::stack_allocator_memory;
52 using coroutine_type = copp::coroutine_context_container<stack_allocator_type>;
56 typedef cotask::task<my_macro_coroutine>
my_task_t;
58 std::vector<my_task_t::ptr_t> task_arr;
61 int my_task_action(
void *) {
72 int main(
int argc,
char *argv[]) {
73 puts(
"###################### task (stack using memory pool) ###################");
74 printf(
"########## Cmd:");
75 for (
int i = 0; i < argc; ++i) {
76 printf(
" %s", argv[i]);
81 max_task_number = atoi(argv[1]);
89 g_stack_size = atoi(argv[3]) * 1024;
91 if (g_stack_size < copp::stack_traits::minimum_size()) {
92 g_stack_size = copp::stack_traits::minimum_size();
99 time_t begin_time = time(
nullptr);
103 task_arr.reserve(
static_cast<size_t>(max_task_number));
104 while (task_arr.size() <
static_cast<size_t>(max_task_number)) {
108 task_arr.push_back(my_task_t::create(my_task_action, alloc, g_stack_size));
111 time_t end_time = time(
nullptr);
113 printf(
"create %d task, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
114 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
117 begin_time = end_time;
118 begin_clock = end_clock;
121 for (
int i = 0; i < max_task_number; ++i) {
122 task_arr[i]->start();
126 bool continue_flag =
true;
127 long long real_switch_times =
static_cast<long long>(0);
129 while (continue_flag) {
130 continue_flag =
false;
131 for (
int i = 0; i < max_task_number; ++i) {
132 if (
false == task_arr[i]->is_completed()) {
133 continue_flag =
true;
135 task_arr[i]->resume();
140 end_time = time(
nullptr);
142 printf(
"switch %d tasks %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
143 real_switch_times,
static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
146 begin_time = end_time;
147 begin_clock = end_clock;
151 end_time = time(
nullptr);
153 printf(
"remove %d tasks, cost time: %d s, clock time: %d ms, avg: %lld ns\n", max_task_number,
154 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
163 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)