Нет описания

armink 7ff2a4c51e 【完善】线程池初始化。 6 лет назад
LICENSE dc7706de89 【增加】初始版本。 6 лет назад
README.md 270f2ed910 【完善】说明文档。 6 лет назад
SConscript be370156dd 【完善】构建脚本。 6 лет назад
thread_pool.c 7ff2a4c51e 【完善】线程池初始化。 6 лет назад
thread_pool.h 8a833b2bb5 【增加】线程池版本号,基于 EasyDataManager 上游。 6 лет назад
thread_pool_sample.c 1ef4c9df76 【完善】日志格式。 6 лет назад

README.md

RT-Thread ̳߳ʵ


1

1.1 ̳߳

̳߳һ̴ֶ߳ʽнӵУȻڴ̺߳ԶЩ

  • ̳߳عThreadPoolManagerڴ̳߳أ
  • ̣߳WorkThread̳߳е̣߳
  • ӿڣTaskÿʵֵĽӿڣԹ̵ִ߳У
  • Уqueueڴûдṩһֻơ

1.2 ̳߳ص

УٶǺܷʱģΪһҪȡڴԴԴJavaиˣͼÿһԱܹڶٺա߷ЧʵһֶξǾܼٴٶĴرһЩܺԴĶ󴴽١жһҪĹؼ⣬ʵһЩ"ػԴ"ԭ̳߳Ҳɴ˶

̳߳صʵԴ https://github.com/armink/EasyDataManager ԴĿ

2 Ӧó

ʹ̳߳Ϊ˼С̱߳ĿӦӰ죬ǰ̱߳ٵĿִ߳ĿDzɺԵġ̱߳ٵĿӦóܿԺԲƣôʹ/ʹ̳߳ضԳ̫ܲӰ졣

  • λʱڴƵʱ϶̣
  • ʵʱҪϸߡյ֮ٴ̣߳޷ʵʱԵҪ󣬴ʱʹ̳߳أ
  • 뾭Ըͻ¼ Web ת޴ʱʹôͳ벻ͣĴ̡߳ʱö̬̳߳ؿԱķ

Ҫͨʹ̳߳أ

  • ̳߳
  • ʼ̳߳
  • ̳߳
  • ̳߳
  • ͬ
  • ͷͬ

3.1 ̳߳

ͨ thread_pool thread_pool_t ̳߳

//ʽһ ṹ
thread_pool pool;

//ʽ ṹָ
thread_pool_t pool;

*עIJʽһ̳߳*

3.2 ʼ̳߳

  • ԭ

    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 ״̬
  • Demo

һ߳Ϊ5ջСΪ1024 Byte̳߳

thread_pool pool;

init_thread_pool(pool, "A", 5, 1024);	

3.3

  • ԭ

    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 ״̬
  • Demo

̳߳ 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);

3.4 ̳߳

  • ԭ

    destroy(thread_pool_t pool);
    
  • API

pool thread_pool_t
ֵ error_code thread_pool_err ״̬
  • Demo

̳߳

pool.destroy(&pool);				

3.5 ͬ

  • ԭ

    sync_lock(thread_pool_t pool)
    
  • API

pool thread_pool_t
ֵ NULL void* ״̬
  • Demo

    pool.lock(&pool);	
    

3.6 ͷͬ

  • ԭ

    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);

̴£

  • thread_pool_sample лʼһ 3 ̵̳߳߳
  • ̳߳һ 5
  • ÿִʱʱ 5S ֵ
  • ȴ 10 S ɾ߳ʣȫ
  • ̳߳

ʹǰ 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

5ϵʽ