thread_delete.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 thread1_entry(void* parameter)
  8. {
  9. rt_uint32_t count = 0;
  10. while (1)
  11. {
  12. rt_kprintf("thread count: %d\n", count ++);
  13. }
  14. }
  15. static void thread2_entry(void* parameter)
  16. {
  17. rt_thread_delay(10);
  18. rt_thread_delete(tid1);
  19. /* delay thread2 to switch to idle thread */
  20. rt_thread_delay(10);
  21. }
  22. int thread_delete_init()
  23. {
  24. tid1 = rt_thread_create("t1",
  25. thread1_entry, (void*)1,
  26. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  27. if (tid1 != RT_NULL)
  28. rt_thread_startup(tid1);
  29. else
  30. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  31. tid2 = rt_thread_create("t2",
  32. thread2_entry, (void*)2,
  33. THREAD_STACK_SIZE, THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  34. if (tid2 != RT_NULL)
  35. rt_thread_startup(tid2);
  36. else
  37. tc_stat(TC_STAT_END | TC_STAT_FAILED);
  38. return 0;
  39. }
  40. #ifdef RT_USING_TC
  41. static void _tc_cleanup()
  42. {
  43. /* lock scheduler */
  44. rt_enter_critical();
  45. /* delete thread */
  46. if (tid1 != RT_NULL && tid1->stat != RT_THREAD_CLOSE)
  47. tc_stat(TC_STAT_FAILED);
  48. if (tid2 != RT_NULL && tid2->stat != RT_THREAD_CLOSE)
  49. tc_stat(TC_STAT_FAILED);
  50. /* unlock scheduler */
  51. rt_exit_critical();
  52. }
  53. int _tc_thread_delete()
  54. {
  55. /* set tc cleanup */
  56. tc_cleanup(_tc_cleanup);
  57. thread_delete_init();
  58. return 100;
  59. }
  60. FINSH_FUNCTION_EXPORT(_tc_thread_delete, a thread delete example);
  61. #else
  62. int rt_application_init()
  63. {
  64. thread_delete_init();
  65. return 0;
  66. }
  67. #endif