/*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 10 system tick*/result= Rt_thread_init (&Thread_test1,"test1", Test1_thread_entry, Rt_null, (rt_uint8_t*) &thread1_stack[0], sizeof(thread1_stack), the, Ten); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test1); } /*Create a static thread: Priority 16, time slice 25 system tick*/result= Rt_thread_init (&Thread_test2,"test2", Test2_thread_entry, Rt_null, (rt_uint8_t*) &thread2_stack[0], sizeof(thread2_stack), -, -); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test2); }}voidTest1_thread_entry (void*parameter) {rt_uint32_t i; /*Infinite Loops*/ while(1) { for(i =0; i<Ten; i++) {rt_kprintf ("%d \ r \ n", i); /*wait for 1s, let go of CPU permissions, switch to other threads*/Rt_thread_delay ( - ); } }}voidTest2_thread_entry (void*parameter) {rt_uint32_t I=0; /*Infinite Loops*/ while(1) {rt_kprintf ("test2 thread count:%d \ r \ n", ++i); /*wait for 0.5s, let go of CPU permissions, switch to other threads*/Rt_thread_delay ( -); }}
Program Run Analysis:
1, first system scheduling test1 thread put into operation, print the NO. 0 run of information, and then through the delay function to suspend themselves 100 time slices, the system will test2 thread scheduling operation;
2, test2 thread print the No. 0 run information, and then through the delay function to suspend themselves 50 time slices;
3, the system has no task to run, the system will be idle thread into operation;
4, 50 time slices after the test2 thread is awakened, print the 1th run of information, and then continue through the delay function to suspend themselves 50 time slices;
5, the system has no task to run, the system will be idle thread into operation;
6, 50 time slices time, test1 thread is awakened, print the 1th run information, continue to suspend 100 time slices;
7, the Test2 thread is awakened, print the 2nd run of information, and then continue through the delay function to suspend themselves 50 time slices;
8, the system has no task to run, the system will be idle thread into operation;
9, 50 time slices after the test2 thread is awakened, print the 3rd run of information, and then continue through the delay function to suspend themselves 50 time slices;
10, the loop executes 5-9 process.
Rt-thread Thread Scheduling