|
|
6 лет назад | |
|---|---|---|
| LICENSE | 6 лет назад | |
| README.md | 6 лет назад | |
| SConscript | 6 лет назад | |
| thread_pool.c | 6 лет назад | |
| thread_pool.h | 6 лет назад | |
| thread_pool_sample.c | 6 лет назад |
̳߳һ̴ֶ߳ʽнӵУȻڴ̺߳ԶЩ
УٶǺܷʱģΪһҪȡڴԴԴJavaиˣͼÿһԱܹڶٺա߷ЧʵһֶξǾܼٴٶĴرһЩܺԴĶ١жһҪĹؼ⣬ʵһЩ"ػԴ"ԭ̳߳Ҳɴ˶
ʹ̳߳Ϊ˼С̱߳ĿӦӰ죬ǰ̱߳ٵĿִ߳ĿDzɺԵġ̱߳ٵĿӦóܿԺԲƣôʹ/ʹ̳߳ضԳ̫ܲӰ졣
Ҫͨʹ̳߳أ
ͨ thread_pool thread_pool_t ̳߳
//ʽһ ṹ
thread_pool pool;
//ʽ ṹָ
thread_pool_t pool;
*עIJʽһ̳߳*
ԭ
init_thread_pool(thread_pool_t const pool,
uint8_t max_thread_num,
uint32_t thread_stack);
API
| pool | thread_pool_t | ||
| max_thread_num | uint8_t | ߳ | |
| thread_stack | uint32_t | ջС | |
| ֵ | error_code | thread_pool_err | ״̬ |
һ߳Ϊ5ջСΪ1024 Byte̳߳
thread_pool pool;
init_thread_pool(pool, "A", 5, 1024);
ԭ
add_task(thread_pool_t const pool,
void (*process)(void *arg),
void *arg);
API
| pool | thread_pool_t | ||
| process(void *arg) | void | ||
| arg | void * | ||
| ֵ | error_code | thread_pool_err | ״̬ |
̳߳ task1ʱͨ־ task1 ̳߳AеЧ
char str_a[]="A";
//
static void *task1(void *arg) {
LOG_D("This is %s test ",(char*)arg);
return NULL;
}
//
pool.addTask(&pool, task1, str_a);
ԭ
destroy(thread_pool_t pool);
API
| pool | thread_pool_t | ||
| ֵ | error_code | thread_pool_err | ״̬ |
̳߳
pool.destroy(&pool);
ԭ
sync_lock(thread_pool_t pool)
API
| pool | thread_pool_t | ||
| ֵ | NULL | void* | ״̬ |
Demo
pool.lock(&pool);
ԭ
sync_unlock(thread_pool_t pool)
API
| pool | thread_pool_t | ||
| ֵ | NULL | void* | ״̬ |
Demo
pool.unlock(&pool);
λ thread_pool_sample.c Դ£
#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);
̴£
ʹǰ menuconfig п THREAD_POOL_USING_SAMPLES ѡ
Finsh/MSH ִ thread_pool_sample ɿʾЧ£
msh />thread_pool_sample
[D/thread_pool] create thread success.Current total thread number is 1
[D/thread_pool] create thread success.Current total thread number is 2
[D/thread_pool] create thread success.Current total thread number is 3
[D/thread_pool] initialize thread pool success!
[D/thread_pool.sample] The task on thread sam_0 is running
[D/thread_pool] add a task to task queue success.
[D/thread_pool.sample] The task on thread sam_1 is running
[D/thread_pool] add a task to task queue success.
[D/thread_pool.sample] The task on thread sam_2 is running
[D/thread_pool] add a task to task queue success.
[D/thread_pool] add a task to task queue success.
[D/thread_pool] add a task to task queue success.
[D/thread_pool.sample] The task on thread sam_1 finish
[D/thread_pool.sample] The task on thread sam_1 is running
[D/thread_pool.sample] The task on thread sam_1 finish
[D/thread_pool.sample] The task on thread sam_1 is running
[D/thread_pool.sample] The task on thread sam_0 finish
[D/thread_pool.sample] The task on thread sam_2 finish
[D/thread_pool.sample] The task on thread sam_1 finish
[D/thread_pool] delete all wait task success
[D/thread_pool] Thread pool destroy success