20 #if defined(PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO) && PROJECT_LIBCOPP_SAMPLE_HAS_CHRONO
22 # define CALC_CLOCK_T std::chrono::system_clock::time_point
23 # define CALC_CLOCK_NOW() std::chrono::system_clock::now()
24 # define CALC_MS_CLOCK(x) static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(x).count())
25 # define CALC_NS_AVG_CLOCK(x, y) \
26 static_cast<long long>(std::chrono::duration_cast<std::chrono::nanoseconds>(x).count() / (y ? y : 1))
28 # define CALC_CLOCK_T clock_t
29 # define CALC_CLOCK_NOW() clock()
30 # define CALC_MS_CLOCK(x) static_cast<int>((x) / (CLOCKS_PER_SEC / 1000))
31 # define CALC_NS_AVG_CLOCK(x, y) (1000000LL * static_cast<long long>((x) / (CLOCKS_PER_SEC / 1000)) / (y ? y : 1))
36 typedef copp::coroutine_context_container<copp::allocator::stack_allocator_malloc>
my_cotoutine_t;
51 my_cotoutine_t::ptr_t *
co_arr =
nullptr;
53 int main(
int argc,
char *argv[]) {
54 puts(
"###################### context coroutine (stack using malloc/free) ###################");
55 printf(
"########## Cmd:");
56 for (
int i = 0; i < argc; ++i) {
57 printf(
" %s", argv[i]);
69 size_t stack_size = 16 * 1024;
71 stack_size = atoi(argv[3]) * 1024;
73 if (stack_size < copp::stack_traits::minimum_size()) {
74 stack_size = copp::stack_traits::minimum_size();
77 time_t begin_time = time(
nullptr);
83 time_t end_time = time(
nullptr);
85 printf(
"allocate %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
MAX_COROUTINE_NUMBER,
86 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
95 end_time = time(
nullptr);
97 printf(
"create %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
MAX_COROUTINE_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;
110 bool continue_flag =
true;
111 long long real_switch_times =
static_cast<long long>(0);
113 while (continue_flag) {
114 continue_flag =
false;
116 if (
false ==
co_arr[i]->is_finished()) {
117 continue_flag =
true;
124 end_time = time(
nullptr);
126 printf(
"switch %d coroutine contest %lld times, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
130 begin_time = end_time;
131 begin_clock = end_clock;
135 end_time = time(
nullptr);
137 printf(
"remove %d coroutine, cost time: %d s, clock time: %d ms, avg: %lld ns\n",
MAX_COROUTINE_NUMBER,
138 static_cast<int>(end_time - begin_time),
CALC_MS_CLOCK(end_clock - begin_clock),
LIBCOPP_COPP_API coroutine_context * get_coroutine() LIBCOPP_MACRO_NOEXCEPT
get current coroutine
int main(int argc, char *argv[])
static int my_runner(void *)
my_cotoutine_t::ptr_t * co_arr
#define CALC_NS_AVG_CLOCK(x, y)
copp::coroutine_context_container< copp::allocator::stack_allocator_malloc > my_cotoutine_t