RT -Thread 学习记录(线程的创建)(懂得都懂,必须支持国产) 嘻嘻!!
通过学习总结在我们以前使用51、32单片机中只有while(1)这一个主线程(所谓的裸机),在接触是实时操作系统后,我们可以创建多个线程,使单片机可以实现同时执行多任务。1.动态线程删除和静态删除用法一样(个人理解,如果有误,请各位大神指点)所以我只是使用了动态线程删除,我在线程2中执行操作删除线程1,之前在线程1里面试过删除线程1,不成功,会报错(搞不明白!(2)参数2 线程的函数指针 线程所要
一、线程的创建

二、动态线程的创建
1.分析RT-Thread 的代码 rt_thread_create函数:
rt_thread_t rt_thread_create(const char *name,
void (*entry)(void *parameter),
void *parameter,
rt_uint32_t stack_size,
rt_uint8_t priority,
rt_uint32_t tick)
/**
* This function will create a thread object and allocate thread object memory
* and stack
* @param name the name of thread, which shall be unique
* @param entry the entry function of thread
* @param parameter the parameter of thread enter function
* @param stack_size the size of thread stack
* @param priority the priority of thread
* @param tick the time slice if there are same priority thread
* @return the created thread object
(1)参数1 线程的名字
(2)参数2 线程的函数指针 线程所要执行的任务
(3)参数3 函数指针的参数
(5)参数4 栈的大小
(6)参数5 线程优先级
(7)参数6 时间片
2.创建过程:
rt_thread_t th1_pr;//创建指针,用于接住动态创建线程的返回值
//线程1
void rt_th1(void *parameter)
{
while(1)
{
rt_kprintf("th1 is running!\r\n");
rt_thread_mdelay(1000);//线程的挂起
}
}
th1_pr=rt_thread_create("th1",rt_th1,NULL,1024,10,10);//动态线程的创建
if(th1_pr==RT_NULL)
{
LOG_E("rt-thread_create1 is fail!\r\n");
return -RT_ENOMEM;
}
rt_thread_startup(th1_pr);//启动线程
三、静态线程的创建
1.内核源码分析:
rt_err_t rt_thread_init(struct rt_thread *thread,
const char *name,
void (*entry)(void *parameter),
void *parameter,
void *stack_start,
rt_uint32_t stack_size,
rt_uint8_t priority,
rt_uint32_t tick)
/**
* This function will initialize a thread, normally it's used to initialize a
* static thread object.
* @param thread the static thread object
* @param name the name of thread, which shall be unique
* @param entry the entry function of thread
* @param parameter the parameter of thread enter function
* @param stack_start the start address of thread stack
* @param stack_size the size of thread stack
* @param priority the priority of thread
* @param tick the time slice if there are same priority thread
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
(1)参数1 静态线程的class
(2)参数2 线程的名字
(3)参数3 线程的函数指针 线程所要执行的任务
(4)参数4 函数指针的参数
(5)参数5 栈的起始地址
(6)参数6 栈的大小
(7)参数7 线程的优先级
(8)参数8 时间片
2.创建过程:
struct rt_thread th2_pr;//创建静态线程的class struct rt_thread *thread
rt_uint8_t th2_start[1024];//rt_uint32_t stack_size 数组名栈的起始地址
int ret;//创建变量,接住静态创建线程的返回值 不是负数 所以是int
//线程2
void rt_th2(void *parameter)
{
//int count=0;
while(1)
{
rt_kprintf("th2 is running!\r\n");
rt_thread_mdelay(1000);
// count++;
// if(count==5)
// rt_thread_delete(th1_pr);
}
}
ret=rt_thread_init(&th2_pr,"th2",rt_th2,NULL,th2_start,1024,15,11);//静态线程的创建
if(ret!=RT_EOK)
{
LOG_E("rt-thread_create2 is fail!\r\n");
return RT_EOK;
}
rt_thread_startup(&th2_pr);//启动线程
三、整体代码:
我在学习中在一个工程中分别使用了动态和静态各创建了线程。
#include <rtthread.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
rt_thread_t th1_pr;//创建指针,用于接住动态创建线程的返回值
struct rt_thread th2_pr;//创建静态线程的class struct rt_thread *thread
rt_uint8_t th2_start[1024];//rt_uint32_t stack_size 栈的开始
int ret;//创建变量(int 型),接住静态创建线程的返回值
//线程1
void rt_th1(void *parameter)
{
while(1)
{
rt_kprintf("th1 is running!\r\n");
rt_thread_mdelay(1000);
}
}
//线程2
void rt_th2(void *parameter)
{
//int count=0;
while(1)
{
rt_kprintf("th2 is running!\r\n");
rt_thread_mdelay(1000);
// count++;
// if(count==5)
// rt_thread_delete(th1_pr);
}
}
int main(void)
{
int count = 1;
th1_pr=rt_thread_create("th1",rt_th1,NULL,1024,10,10);//动态线程的创建
if(th1_pr==RT_NULL)
{
LOG_E("rt-thread_create1 is fail!\r\n");
return -RT_ENOMEM;
}
rt_thread_startup(th1_pr);//启动线程
ret=rt_thread_init(&th2_pr,"th2",rt_th2,NULL,th2_start,1024,15,11);//静态线程的创建
if(ret!=RT_EOK)
{
LOG_E("rt-thread_create2 is fail!\r\n");
return RT_EOK;
}
rt_thread_startup(&th2_pr);//启动线程
while (count++)
{
LOG_D("Hello RT-Thread!");
rt_thread_mdelay(1000);
}
return RT_EOK;
}
运行结果:

四、线程的删除
1.动态线程删除和静态删除用法一样(个人理解,如果有误,请各位大神指点)所以我只是使用了动态线程删除,我在线程2中执行操作删除线程1,之前在线程1里面试过删除线程1,不成功,会报错(搞不明白!!希望各位大神指点指点)。
void rt_th2(void *parameter)
{
int count=0;
while(1)
{
rt_kprintf("th2 is running!\r\n");
rt_thread_mdelay(1000);
count++;
if(count==5)
rt_thread_delete(th1_pr);
}
}
运行结果:

两个线程执行5秒后,删除线程1。
五、总结
通过学习总结在我们以前使用51、32单片机中只有while(1)这一个主线程(所谓的裸机),在接触是实时操作系统后,我们可以创建多个线程,使单片机可以实现同时执行多任务。本人是刚刚接触实时操作系统,希望有什么问题,希望各位大神指点。感谢感谢!!!
openvela 操作系统专为 AIoT 领域量身定制,以轻量化、标准兼容、安全性和高度可扩展性为核心特点。openvela 以其卓越的技术优势,已成为众多物联网设备和 AI 硬件的技术首选,涵盖了智能手表、运动手环、智能音箱、耳机、智能家居设备以及机器人等多个领域。
更多推荐



所有评论(0)