demo.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. 此demo用于演示动态线程创建
  3. */
  4. #include <rtthread.h>
  5. #include "board.h"
  6. #ifdef RT_USING_FINSH
  7. #include <finsh.h>
  8. #include <shell.h>
  9. #endif
  10. static rt_thread_t tid1 = RT_NULL;
  11. static rt_thread_t tid2 = RT_NULL;
  12. static void thread1_entry(void* parameter)
  13. {
  14. rt_uint32_t count = 0;
  15. rt_kprintf("thread1 dynamicly created ok\n");
  16. while (1)
  17. {
  18. rt_kprintf("thread1 count: %d\n",count++);
  19. rt_thread_delay(RT_TICK_PER_SECOND);
  20. }
  21. }
  22. static void thread2_entry(void* parameter)
  23. {
  24. rt_uint32_t count = 0;
  25. rt_kprintf("thread2 dynamicly created ok\n");
  26. while(1)
  27. {
  28. if(count == 3)
  29. break;
  30. rt_kprintf("thread2 count: %d\n",count++);
  31. rt_thread_delay(RT_TICK_PER_SECOND);
  32. }
  33. rt_thread_delay(RT_TICK_PER_SECOND * 4);
  34. rt_thread_delete(tid1);
  35. rt_kprintf("thread1 deleted ok\n");
  36. }
  37. int demo_init(void)
  38. {
  39. tid1 = rt_thread_create("thread1",
  40. thread1_entry,
  41. RT_NULL,
  42. 512, 6, 10);
  43. if (tid1 != RT_NULL)
  44. rt_thread_startup(tid1);
  45. tid2 = rt_thread_create("thread2",
  46. thread2_entry,
  47. RT_NULL,
  48. 512, 6, 10);
  49. if (tid2 != RT_NULL)
  50. rt_thread_startup(tid2);
  51. return 0;
  52. }