当前位置:网站首页>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 */
}
}
边栏推荐
- digital signal processing
- Chrome浏览器取消输入框记录表单输入历史
- Chapter 09 English printed character recognition based on feature matching matlab deep learning practical case
- 基于DPDK的高效包处理系统
- Sword finger offer 26: substructure of tree
- [Chapter 26 medical impact segmentation system based on minimum error method and region growth -- matlab deep learning practical GUI project]
- How does the QT program implement the default selection of a behavior in the selected listwidget
- excel常用快捷键excel快捷键汇总
- 微信小程序影视评论交流平台系统毕业设计毕设(8)毕业设计论文模板
- An error occurs when Android uses the SQL database to login. Attempt to invoke virtual method '' on a null object reference
猜你喜欢

Games-101 personal summary transformation

Test APK exception control WiFi scan attacker development

How to restore the IE browser auto jump edge

基于DPDK的高效包处理系统

本周一问 | -leaf 这个属性的含义?

pdf转word pdf转图片 图片转pdf 修改pdf文件就像操作Word一样方便(Acrobat DC使用介绍)

微信小程序影视评论交流平台系统毕业设计毕设(7)中期检查报告
![LeetCode 513 找树左下角的值[BFS 二叉树] HERODING的LeetCode之路](/img/15/b406e7bf1b83cbdd685c8cde427786.png)
LeetCode 513 找树左下角的值[BFS 二叉树] HERODING的LeetCode之路

Mba-day24 best value problem

微信小程序影视评论交流平台系统毕业设计毕设(3)后台功能
随机推荐
Chapter 09 English printed character recognition based on feature matching matlab deep learning practical case
Courses learned
学习过的课程
Mysql database easy learning 09 - commonly used by data analysts: multi table query of data query language DQL
中午不睡觉下午困
Learn to crawl steadily 08 - detailed explanation of the use method of selenium
postgresql根据时间字段的大小来取数
Games-101-personal summary shading
Word document to markdown document?
Chapter 24 image and video processing based on Simulink -- matlab in-depth learning and practical collation
Rational Rose installation tutorial
word中mathtype公式右编号右对齐
微信小程序影视评论交流平台系统毕业设计毕设(7)中期检查报告
新员工入职,了解工作信息
Flexer series: indexedstack in Flexer
idea----bookmark
手机app测试方法
SQL Server recursive query
Chapter 18 build a general video processing tool based on GUI matlab application GUI implementation
微信小程序影视评论交流平台系统毕业设计毕设(2)小程序功能