thread_example.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * RT-Thread RuiChing
  3. *
  4. * COPYRIGHT (C) 2024-2025 Shanghai Real-Thread Electronic Technology Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * The license and distribution terms for this file may be
  8. * found in the file LICENSE in this distribution.
  9. */
  10. #include <rtthread.h>
  11. #define THREAD_PRIORITY 25
  12. #define THREAD_STACK_SIZE 4096
  13. #define THREAD_TIMESLICE 5
  14. static rt_thread_t tid1 = RT_NULL;
  15. rt_align(RT_ALIGN_SIZE)
  16. static char thread2_stack[1024];
  17. static struct rt_thread thread2;
  18. static void thread1_entry(void *parameter)
  19. {
  20. rt_uint32_t count = 0;
  21. for (count = 0; count < 10 ; count++)
  22. {
  23. rt_kprintf("thread1 count: %d\n", count);
  24. rt_thread_mdelay(100);
  25. }
  26. rt_kprintf("thread1 exit\n");
  27. }
  28. static void thread2_entry(void *param)
  29. {
  30. rt_uint32_t count = 0;
  31. for (count = 0; count < 10 ; count++)
  32. {
  33. rt_kprintf("thread2 count: %d\n", count);
  34. }
  35. rt_kprintf("thread2 exit\n");
  36. }
  37. int thread_example(void)
  38. {
  39. tid1 = rt_thread_create("thread1", thread1_entry, RT_NULL,
  40. THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
  41. if (tid1 != RT_NULL)
  42. {
  43. rt_thread_startup(tid1);
  44. }
  45. rt_thread_delay(10);
  46. rt_thread_init(&thread2, "thread2", thread2_entry, RT_NULL,
  47. &thread2_stack[0], sizeof(thread2_stack), THREAD_PRIORITY - 1,
  48. THREAD_TIMESLICE);
  49. rt_thread_startup(&thread2);
  50. return 0;
  51. }
  52. MSH_CMD_EXPORT(thread_example, thread_example);