awtk/tests/action_thread_pool_test.cpp

52 lines
1019 B
C++
Raw Normal View History

2020-02-09 17:35:14 +08:00
#include "tkc/utils.h"
#include "tkc/thread.h"
#include "tkc/platform.h"
#include "tkc/action_thread_pool.h"
2020-02-10 11:52:38 +08:00
#include <atomic>
2020-02-09 17:35:14 +08:00
2020-02-13 10:19:58 +08:00
#define NR 10000 * 100
2020-02-10 11:52:38 +08:00
static std::atomic<int> exec_times;
2020-02-09 17:35:14 +08:00
2020-02-09 18:22:13 +08:00
static ret_t qaction_dummy_on_event(qaction_t* action, event_t* e) {
2020-02-10 10:13:40 +08:00
if (e->type == EVT_DONE) {
2020-02-09 18:22:13 +08:00
log_debug("done\n");
2020-02-13 10:19:58 +08:00
qaction_destroy(action);
2020-02-09 18:22:13 +08:00
}
return RET_OK;
}
static ret_t qaction_dummy_exec(qaction_t* action) {
2020-02-09 17:35:14 +08:00
exec_times++;
2020-02-10 11:52:38 +08:00
log_debug("exec: exec_times=%d\n", exec_times.load());
2020-02-09 17:35:14 +08:00
return RET_OK;
}
void test() {
uint32_t i = 0;
2020-02-13 10:19:58 +08:00
action_thread_pool_t* pool = action_thread_pool_create(10, 2);
2020-02-09 18:22:13 +08:00
2020-02-13 10:19:58 +08:00
for (i = 0; i < NR; i++) {
qaction_t* a = qaction_create(qaction_dummy_exec, NULL, 0);
qaction_set_on_event(a, qaction_dummy_on_event);
2020-02-10 11:52:38 +08:00
action_thread_pool_exec(pool, a);
}
2020-02-10 10:13:40 +08:00
sleep_ms(2000);
2020-02-09 18:41:04 +08:00
2020-02-09 17:35:14 +08:00
action_thread_pool_destroy(pool);
2020-02-10 11:52:38 +08:00
log_debug("exec_times=%d\n", exec_times.load());
2020-02-09 17:35:14 +08:00
}
#include "tkc/platform.h"
int main(int argc, char* argv[]) {
platform_prepare();
test();
return 0;
}