awtk/tests/async_call_test.cpp

53 lines
958 B
C++
Raw Normal View History

2021-09-06 18:02:51 +08:00
#include "tkc/utils.h"
#include "tkc/thread.h"
#include "tkc/async.h"
#include "tkc/platform.h"
#include "tkc/action_thread_pool.h"
static uint32_t fibonacci(uint32_t n) {
if (n == 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
static ret_t fibonacci_async_exec(void* ctx) {
uint32_t n = (uint32_t)tk_pointer_to_int(ctx);
fibonacci(n);
log_debug("thread id=%u n=%u\n", (uint32_t)tk_thread_self(), n);
return RET_OK;
}
static ret_t fibonacci_async(uint32_t n) {
return async_call(fibonacci_async_exec, NULL, tk_pointer_from_int(n));
}
#define NR 10000
void test() {
uint32_t i = 0;
for (i = 0; i < NR; i++) {
uint32_t n = i%30+10;
log_debug("%u %u\n", i, n);
fibonacci_async(n);
}
}
#include "tkc/platform.h"
int main(int argc, char* argv[]) {
platform_prepare();
async_call_init_ex(10, 2);
test();
async_call_deinit();
return 0;
}