当前位置:网站首页>rt_ Thread thread management

rt_ Thread thread management

2022-06-22 02:13:00 Early rising sun

One 、 summary

  • RT-Thread A thread can be considered as a collection of a series of independent threads .
  • Each thread in Run in your own environment . At any moment , Only one thread gets run ,RT-Thread The scheduler decides which thread to run .
  • Every RT-Thread Threads have their own stack .
  • RT-Thread The thread module can provide users with multiple threads , The switch between threads and signal communication , Help users manage business process .
  • RT-Thread Threads in are preemptive scheduling mechanisms , At the same time, it supports time slice rotation scheduling .
  • High priority threads can interrupt low priority threads , Low priority threads must be blocked on high priority threads The plug or the end can be dispatched .
  • RT-Thread The thread scheduler provided in is full preemptive scheduling based on priority : Divide in the system The interrupt handling function 、 The code of the locking part of the scheduler and the code of prohibiting interruption are non preemptive , Other parts of the system can be preempted , Including the thread scheduler itself .
  • RT-Thread The thread scheduler provided in is full preemptive scheduling based on priority : Divide in the system The interrupt handling function 、 The code of the locking part of the scheduler and the code of prohibiting interruption are non preemptive , Other parts of the system can be preempted , Including the thread scheduler itself .
  • rt_thread In real time : When all ready threads are linked in their corresponding priority queue , The decision-making process will evolve into finding the non empty linked list with the highest priority thread in the priority array .RT-Thread The priority algorithm based on bitmap is adopted in the kernel ( Time complexity O(1), That is, it has nothing to do with the number of ready threads ), Get the line with the highest priority quickly through the positioning of bitmap cheng .
  • RT-Thread The kernel also allows the creation of threads of the same priority . When a thread of the same priority is adopted The inter slice rotation mode is used for scheduling ( That is to say, time-sharing scheduler ), Time slice rotation scheduling is only available when It is valid only when there is no higher priority ready thread in the previous system .
  • The principle of thread scheduling is that once the thread state changes , And currently running When the thread priority of is less than the highest priority of the thread in the priority queue group , Switch threads immediately ( Unless The current system is in the state of interrupt handler or thread switching is prohibited ).
  • because RT-Thread Scheduler The implementation of is in the form of priority linked list , Therefore, the number of bus paths in the system is not limited , Only with the system Memory resources that can be provided . In order to ensure the real-time of the system , The system ensures high priority as much as possible Level threads can run .

Two 、 State of thread

1、 Initial state (RT_THREAD_INIT)

When creating a thread , Set the state of the thread to the initial state .

2、 The ready state (RT_THREAD_READY)

This thread is in the ready list , Ability to execute , wait for cpu

3、 Running state (RT_THREAD_RUNNING)

This thread is running , Take up the processor .

4、 Pending state (RT_THREAD_SUSPEND)

This thread is waiting for a timing or external interrupt , Rerouting is not in the ready list , Containing threads are Hang up 、 Thread is delayed 、 Thread is waiting for semaphore 、 Read write queue or wait for read write events .

5、 Closed (RT_THREAD_CLOSE)

The thread is finished , Wait for the system to recycle resources .

3、 ... and 、 Thread state switch instance

#include "board.h"
#include "rtthread.h"


/*  Define thread control blocks  */
static rt_thread_t led1_thread = RT_NULL;
/*  Thread body function  */
static void led1_thread_entry(void* parameter);

/*  Define thread control blocks  */
static rt_thread_t key_thread = RT_NULL;
/*  Thread body function  */
static void key_thread_entry(void* parameter);

/*******************************************************************************
*  Letter   Count   name          : main
*  The functionality 		   :  The main function 
*  transport      Enter into          :  nothing 
*  transport      Out          :  nothing 
*******************************************************************************/
int main()
{
	rt_kprintf(" Thread management process experiment !\r\n");
	rt_kprintf(" Press down KEY_UP Hang up LED Threads , Press down KEY1 recovery LED Threads !\r\n");
	
	// establish LED1 Threads 
	led1_thread =rt_thread_create( 
					"led1",              /*  Thread name  */
                     led1_thread_entry,   /*  Thread entry function  */
                     RT_NULL,             /*  Thread entry function parameters  */
                     512,                 /*  Thread stack size  */
                     3,                   /*  Thread priority  */
                     20);                 /*  Thread timeslice  */
	
	/*  Start thread , Turn on scheduling  */
    if(led1_thread != RT_NULL)
		rt_thread_startup(led1_thread);
    else
		return -1;
	
	// establish KEY Threads 
	key_thread =rt_thread_create( 
					"key",              /*  Thread name  */
                     key_thread_entry,   /*  Thread entry function  */
                     RT_NULL,             /*  Thread entry function parameters  */
                     512,                 /*  Thread stack size  */
                     2,                   /*  Thread priority  */
                     20);                 /*  Thread timeslice  */
	
	/*  Start thread , Turn on scheduling  */
    if(key_thread != RT_NULL)
		rt_thread_startup(key_thread);
    else
		return -1;
	
}

//LED1 Threads 
static void led1_thread_entry(void* parameter)
{	
    while(1)
    {
        LED1=0;
        rt_thread_delay(200);   /*  Time delay 200 individual tick */
		rt_kprintf("led1_thread running,LED1_ON\r\n");
		LED1=1;     
        rt_thread_delay(500);   /*  Time delay 500 individual tick */
		rt_kprintf("led1_thread running,LED1_OFF\r\n");
    }
}

//KEY Threads 
static void key_thread_entry(void* parameter)
{
	u8 key=0;
	rt_err_t erflag=0;
	
	while(1)
	{
		key=KEY_Scan(0);
		if(key==KEY_UP_PRESS)
		{
			rt_kprintf("LED1 Thread hanging !\r\n");
			erflag=rt_thread_suspend(led1_thread);
			if(erflag==RT_EOK)
				rt_kprintf(" Thread suspended successfully !\r\n");
			else
				rt_kprintf(" Thread suspend failed !\r\n");
		}
		else if(key==KEY1_PRESS)
		{
			rt_kprintf("LED1 Thread recovery !\r\n");
			erflag=rt_thread_resume(led1_thread);
			if(erflag==RT_EOK)
				rt_kprintf(" Thread recovery succeeded !\r\n");
			else
				rt_kprintf(" Thread recovery failed !\r\n");
		}
		rt_thread_delay(20);   /*  Time delay 20 individual tick */
	}
}

原网站

版权声明
本文为[Early rising sun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220152378138.html