Rt-thread scheduling for the same priority thread

Source: Internet
Author: User

/*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 2 system tick*/result= Rt_thread_init (&Thread_test1,"test1", Test1_thread_entry, Rt_null, (rt_uint8_t*) &thread1_stack[0],sizeof(Thread1_stack), the,2); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test1); }    /*Create a static thread: Priority 15, time slice 1 system tick*/result= Rt_thread_init (&Thread_test2,"test2", Test2_thread_entry, Rt_null, (rt_uint8_t*) &thread2_stack[0],sizeof(Thread2_stack), the,1); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test2); }}voidTest1_thread_entry (void*parameter)    {rt_uint8_t i;  for(i =0; I <6; i + +) {rt_kprintf ("thread1:\r\n"); rt_kprintf ("This was a demo for same priority!\r\n"); Rt_thread_delay (4); }}voidTest2_thread_entry (void*parameter)    {rt_uint8_t J;  for(j =0; J < -; J + +) {rt_kprintf ("thread2:\r\n"); rt_kprintf ("This was a demo for same priority!\r\n"); }}

The output of the thread test2 is incomplete, indicating that the execution of the test2 thread is interrupted because the test1 thread and the test2 thread have a priority of 15, and there is no preemption, so the test2 thread waits until its own execution time slice arrives, the system deprives the CPU of the right to use, Instead, the use is handed over to the test1 thread, which test1 the thread to regain execution. It can be seen that when two of the same threads run on a time slice basis, the time slice arrives, and the CPU is handed over to the next ready-to-be-prioritized thread.
PS: Since none of the above two threads is an infinite loop structure, its thread state becomes initialized after its normal exit, and then it is removed from the thread schedule list in the idle thread. Learn to create a task here and call Rt_thread_startup later to become ready. The code is as follows:

#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[ +];Staticrt_uint8_t thread3_stack[ +];/*thread control blocks for static threads*/Static structRt_thread Thread_test1;Static structRt_thread Thread_test2;Static structRt_thread thread_test3;Static voidTest1_thread_entry (void*parameter);Static voidTest2_thread_entry (void*parameter);Static voidTest3_thread_entry (void*parameter);Staticrt_uint8_t Test3_thread_flag =0;voidDemo_thread_creat (void) {rt_err_t result; /*Create a static thread: Priority 15, time slice 2 system tick*/result= Rt_thread_init (&Thread_test1,"test1", Test1_thread_entry, Rt_null, (rt_uint8_t*) &thread1_stack[0],sizeof(Thread1_stack), the,2); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test1); }    /*Create a static thread: Priority 15, time slice 1 system tick*/result= Rt_thread_init (&Thread_test2,"test2", Test2_thread_entry, Rt_null, (rt_uint8_t*) &thread2_stack[0],sizeof(Thread2_stack), the,1); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test2); }    /*Create a static thread: Priority 14, time slice 1 system tick*/result= Rt_thread_init (&Thread_test3,"test3", Test3_thread_entry, Rt_null, (rt_uint8_t*) &thread3_stack[0],sizeof(Thread3_stack), -,1); if(Result = =Rt_eok) {Rt_thread_startup (&thread_test3); }}voidTest1_thread_entry (void*parameter)    {rt_uint8_t i;  for(i =0; I <6; i + +) {rt_kprintf ("thread1:\r\n"); rt_kprintf ("This was a demo for same priority!\r\n"); Rt_thread_delay (4); }}voidTest2_thread_entry (void*parameter)    {rt_uint8_t J;  for(j =0; J < -; J + +) {rt_kprintf ("thread2:\r\n"); rt_kprintf ("This was a demo for same priority!\r\n"); }}voidTest3_thread_entry (void*parameter) {     while(1) {Rt_thread_delay ( the); if(0==Test3_thread_flag) {Rt_thread_startup (&thread_test1); Rt_thread_startup (&thread_test2); Test3_thread_flag=1; }    }}


This routine also needs to close the Finsh component.

Rt-thread scheduling for the same priority thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.