A thread that is in a ready state at the same time, with a high priority executed first.
When high priority is ready, the low priority task yields the CPU, allowing the high-priority task to execute first.
Create two task functions:
//Thread-Priority preemptionvoidThread1_entry (void*parameter) {rt_uint32_t Count=0; while(1) { for(;; count++) {Rt_thread_delay (3*rt_tick_per_second);//wait three seconds to output oncert_kprintf ("count =%d\n", Count); } }}voidThread2_entry (void*parameter) {rt_tick_t tick; rt_uint32_t I=0; for(i =0;; i++) {Tick=Rt_tick_get (); Rt_thread_delay (Rt_tick_per_second); //and so the vaccine output oncert_kprintf ("tick =%d\n", tick); }}
Start them:
voidRt_thread_test (void){ //Thread preemption Experimentresult= Rt_thread_init (&thread1,"Thread1", Thread1_entry,rt_null,thread1_stack, +,Ten,Ten); if(Result = =Rt_eok) {Rt_thread_startup (&thread1); } if(Rt_eok = = Rt_thread_init (&thread2,"thread2", Thread2_entry,rt_null,thread2_stack, +,Ten,Ten) {Rt_thread_startup (&thread2); }}
Because of the higher priority, Thread1 first gets executed, then it calls the delay, time is 3 system tick, so thread2 is executed. You can find a rule from the printing results, the first time thread2 print two times Thread1 will print once, then thread2 each print three times Thread1 will print once. Analysis of two-thread entry program can be found that in the Thread1 3 system tick delay, thread2 actually get three execution opportunities, but obviously in the first delay of Thread1 thread2 the third execution and no execution end, after the third delay end, Thread2 should have performed the third print count, but since Thread1 at this time the delay is also over, and its priority is higher than thread2, so preemption thread2 execution and start execution. When the thread1 again into the delay, the previously preempted thread2 print to continue, and then after two times 1 system tick delay and two print count, after the third system tick ended again encountered thread1 delay end, Thread1 again preemption to get execution, So before this thread1 print, the thread2 performed three print counts.
Thread Priority preemption Experiment "Rt-thread learning Note 3"