[RTT例程练习] 1.5 优先级相同线程轮转调度
之前说过,相同优先级的线程,在自己的时间片用光之后,会被剥夺调度器,让给同优先级的其他线程。
程序:
#include <rtthread.h> static struct rt_thread thread1; static struct rt_thread thread2; static char thread1_stack[512]; static char thread2_stack[512]; //static rt_uint32_t t1_count = 0; //static rt_uint32_t t2_count = 0; static void thread1_entry(void* parameter) { rt_uint8_t i; for(i = 0; i < 6; i ++) { rt_kprintf("Thread11111111111111111111111111:\n\r"); rt_kprintf("This is \n"); rt_kprintf("a\n"); rt_kprintf("demo\n"); rt_thread_delay(10); } } static void thread2_entry(void* parameter) { rt_uint8_t j; for(j = 0; j <60; j ++) { rt_kprintf("Thread2:\n\r"); rt_kprintf("This is \n"); rt_kprintf("a\n"); rt_kprintf("demo\n"); } } int rt_application_init() { rt_err_t result; result = rt_thread_init(&thread1, "t1", thread1_entry, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), 6, 10); if (result == RT_EOK) rt_thread_startup(&thread1); result = rt_thread_init(&thread2, "t2", thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), 6, 5); if (result == RT_EOK) rt_thread_startup(&thread2); return result; } /*@}*/
输出结果:
Thread1: This is a demo Thread2: This is a demo Thread2: This is a demo Thread2: This is a demo Thread2: This is a demo Thread2: This is a demo Thread2: This isThread11111111111111111111111111: This is a demo a demo Thread2: This is a demo Thread2: This is a demo Thread2: This is a demo Thread2: This is a Demo
可以看到thread2 的时间片用光,插入了thread1 的打印内容。