thread_dynamic_simple.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <rtthread.h>
  2. #include "tc_comm.h"
  3. /*
  4. * This is an example for dynamic thread
  5. */
  6. static rt_thread_t tid1 = RT_NULL, tid2 = RT_NULL;
  7. static void thread_entry(void* parameter)
  8. {
  9. rt_uint32_t count = 0;
  10. rt_uint32_t no = (rt_uint32_t) parameter;
  11. while (1)
  12. {
  13. rt_kprintf("thread%d count: %d\n", no, count ++);
  14. rt_thread_delay(10);
  15. }
  16. }
  17. int thread_dynamic_simple_init()
  18. {
  19. tid1 = rt_thread_create("t1",
  20. thread_entry, (void*)1,
  21. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  22. if (tid1 != RT_NULL)
  23. rt_thread_startup(tid1);
  24. else
  25. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  26. tid2 = rt_thread_create("t2",
  27. thread_entry, (void*)2,
  28. THREAD_STACK_SIZE, THREAD_PRIORITY + 1, THREAD_TIMESLICE);
  29. if (tid2 != RT_NULL)
  30. rt_thread_startup(tid2);
  31. else
  32. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  33. return 0;
  34. }
  35. #ifdef RT_USING_TC
  36. static void _tc_cleanup()
  37. {
  38. /* lock scheduler */
  39. rt_enter_critical();
  40. /* delete thread */
  41. if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
  42. rt_thread_delete(tid1);
  43. if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
  44. rt_thread_delete(tid2);
  45. /* unlock scheduler */
  46. rt_exit_critical();
  47. tc_done(TC_STAT_PASSED);
  48. }
  49. int _tc_thread_dynamic_simple()
  50. {
  51. /* set tc cleanup */
  52. tc_cleanup(_tc_cleanup);
  53. thread_dynamic_simple_init();
  54. return 100;
  55. }
  56. FINSH_FUNCTION_EXPORT(_tc_thread_dynamic_simple, a dynamic thread example);
  57. #else
  58. int rt_application_init()
  59. {
  60. thread_dynamic_simple_init();
  61. return 0;
  62. }
  63. #endif