-
Notifications
You must be signed in to change notification settings - Fork 3
/
thread_pool_sample.c
46 lines (38 loc) · 1.39 KB
/
thread_pool_sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* This file is part of the EasyDataManager Library.
*
* Copyright (C) 2019 by Armink <armink.ztl@gmail.com>
*
* Function: a thread pool base on RT-Thread
* Created on: 2019-03-29
*/
#include <stdlib.h>
#define DBG_SECTION_NAME "thread_pool.sample"
#define DBG_LEVEL DBG_LOG
#include <rtdbg.h>
#ifdef PKG_USING_THREAD_POOL
#include <finsh.h>
#include "thread_pool.h"
static void task(void *arg) {
LOG_I("The task on thread %.*s is running.", RT_NAME_MAX, rt_thread_self()->name);
rt_thread_delay(rt_tick_from_millisecond((uint32_t)arg));
LOG_I("The task on thread %.*s will finish.", RT_NAME_MAX, rt_thread_self()->name);
}
static void thread_pool_sample(uint8_t argc, char **argv) {
thread_pool pool;
init_thread_pool(&pool, "sam", 3, 1024);
/* add 5 task to thread pool */
pool.add_task(&pool, task, (void *)(rand() % 5000));
pool.add_task(&pool, task, (void *)(rand() % 5000));
pool.add_task(&pool, task, (void *)(rand() % 5000));
pool.add_task(&pool, task, (void *)(rand() % 5000));
pool.add_task(&pool, task, (void *)(rand() % 5000));
/* wait 10S */
rt_thread_delay(rt_tick_from_millisecond(10 * 1000));
/* delete all task */
pool.del_all(&pool);
/* destroy the thread pool */
pool.destroy(&pool);
}
MSH_CMD_EXPORT(thread_pool_sample, Run thread pool sample);
#endif /* PKG_USING_THREAD_POOL */