Rt-thread a thread's surrender

Source: Internet
Author: User

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

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.