thread_pool_sample.c 1.4 KB

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