The previous two examples show that the thread scheduling is a thread switching by the system "active intervention", in fact, we can also use the initiative to let the CPU use the actual situation. The system function in Rt-thread: Rt_thread_yield () allows the thread that invokes it to temporarily give up the CPU's use, leaving the next highest priority thread to run, but the thread that calls Rt_thread_yield () still holds the ready state. This and "Kong Rong let pear" a bit like: this pear I do not eat, the next pear I will be rude.
#include <rtthread.h>#include<stm32f10x.h>#include"test.h"/*Variable allocation 4-byte alignment*/ALIGN (rt_align_size)/*thread stacks for static threads*/Staticrt_uint8_t thread1_stack[ +];Staticrt_uint8_t thread2_stack[ +];/*thread control blocks for static threads*/Static structRt_thread Thread_test1;Static structRt_thread Thread_test2;Static voidTest1_thread_entry (void*parameter);Static voidTest2_thread_entry (void*parameter);voidDemo_thread_creat (void) {rt_err_t result; /*Create a static thread: Priority 15, time slice 5 system tick*/result= Rt_thread_init (&Thread_test1,"test1", Test1_thread_entry, Rt_null, (rt_uint8_t*) &thread1_stack[0],sizeof(Thread1_stack), the,5); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test1); } /*Create a static thread: Priority 15, time slice 5 system tick*/result= Rt_thread_init (&Thread_test2,"test2", Test2_thread_entry, Rt_null, (rt_uint8_t*) &thread2_stack[0],sizeof(Thread2_stack), the,5); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test2); }}voidTest1_thread_entry (void*parameter) {rt_uint32_t Count=0; while(1) { /*print the output of thread 1*/rt_kprintf ("Thread1:count =%d\n", Count + +); /*you should switch to test2 execution after yield execution*/Rt_thread_yield (); }}voidTest2_thread_entry (void*parameter) {rt_uint32_t Count=0; while(1) { /*you should switch to test1 execution after yield execution*/Rt_thread_yield (); /*print the output of thread 2*/rt_kprintf ("Thread2:count =%d\n", Count + +); }}
Rt-thread a thread's surrender