线程#
线程的创建与启动#
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-02-06 RT-Thread first version
*/
#include <rtthread.h> // 包含RT-Thread的核心头文件
#define DBG_TAG "main" // 定义日志标签
#define DBG_LVL DBG_LOG // 定义日志级别为普通日志
#include <rtdbg.h> // 包含调试日志的头文件
static rt_thread_t tid1 = RT_NULL; // 定义线程句柄,初始化为NULL
// 线程1的入口函数
static void thread1_entry(void *parameter)
{
int count = 0; // 定义一个计数器变量
// 无限循环
while (1)
{
// 打印线程运行信息和当前计数
rt_kprintf("Thread 1 is running, count: %d\n", count);
// 增加计数器
count++;
// 线程延迟1秒(1000毫秒)
rt_thread_mdelay(1000);
}
}
// 主函数
int main(void)
{
// 动态创建线程1
// 参数说明:
// "thread1": 线程名称
// thread1_entry: 线程入口函数
// RT_NULL: 线程参数
// 512: 线程栈大小(单位:字节)
// 20: 线程优先级(数值越小,优先级越高)
// 10: 线程时间片
tid1 = rt_thread_create("thread1", thread1_entry, RT_NULL, 512, 20, 10);
// 如果线程1创建成功,则启动线程1
if (tid1 != RT_NULL)
{
rt_thread_startup(tid1);
}
// 主函数返回0,表示正常结束
return 0;
}
实现效果:

遇到的问题:#
1.板子型号匹配导致无法正常发送
2.RT-thread assertion failed at function:rt_application_init
解决方法:

切换线程#
两个线程 thread1 和 thread2 轮流执⾏,各⾃输出计数。由于它们的优先级和时间⽚相同,调度器会公平地让它们轮流⼯作
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-02-06 RT-Thread first version
*/
#include <rtthread.h> // 包含RT-Thread的核心头文件
#define DBG_TAG "main" // 定义日志标签
#define DBG_LVL DBG_LOG // 定义日志级别为普通日志
#include <rtdbg.h> // 包含调试日志的头文件
// 定义线程1和线程2的句柄,初始化为NULL
static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
// 线程1的入口函数
void thread1_entry(void *parameter)
{
int count = 0; // 定义一个计数器变量
// 循环计数,从0到9
while (count < 10)
{
// 打印线程1的运行信息和当前计数
rt_kprintf("Thread 1 is running, count: %d\n", count);
// 增加计数器
count++;
// 线程延迟1秒(1000毫秒)
rt_thread_mdelay(1000);
}
}
// 线程2的入口函数
static void thread2_entry(void *parameter)
{
int count = 0; // 定义一个计数器变量
// 循环计数,从0到9
while (count < 10)
{
// 打印线程2的运行信息和当前计数
rt_kprintf("Thread 2 is running, count: %d\n", count);
// 增加计数器
count++;
// 线程延迟1秒(1000毫秒)
rt_thread_mdelay(1000);
}
}
// 主函数
int main(void)
{
// 动态创建线程1
// 参数说明:
// "thread1": 线程名称
// thread1_entry: 线程入口函数
// RT_NULL: 线程参数
// 1024: 线程栈大小(单位:字节)
// 25: 线程优先级(数值越小,优先级越高)
// 5: 线程时间片
tid1 = rt_thread_create("thread1", thread1_entry, RT_NULL, 1024, 25, 5);
// 动态创建线程2
// 参数说明:
// "thread2": 线程名称
// thread2_entry: 线程入口函数
// RT_NULL: 线程参数
// 1024: 线程栈大小(单位:字节)
// 25: 线程优先级(数值越小,优先级越高)
// 5: 线程时间片
tid2 = rt_thread_create("thread2", thread2_entry, RT_NULL, 1024, 25, 5);
// 如果线程1创建成功,则启动线程1
if (tid1 != RT_NULL)
{
rt_thread_startup(tid1);
}
// 如果线程2创建成功,则启动线程2
if (tid2 != RT_NULL)
{
rt_thread_startup(tid2);
}
// 主函数返回0,表示正常结束
return 0;
}
实现效果:

定时器管理#
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-02-06 RT-Thread first version
*/
#include <rtthread.h> // 包含RT-Thread的核心头文件
#define DBG_TAG "main" // 定义日志标签
#define DBG_LVL DBG_LOG // 定义日志级别为普通日志
#include <rtdbg.h> // 包含调试日志的头文件
static rt_timer_t timer1; // 定义定时器1,周期触发
static rt_timer_t timer2; // 定义定时器2,单次触发
// 定时器1的超时回调函数
static void timer1out(void *parameter)
{
// 输出日志:定时器1超时
rt_kprintf("timer1 timeout\n");
}
// 定时器2的超时回调函数
static void timer2out(void* parameter)
{
// 输出日志:定时器2超时
rt_kprintf("timer2 timeout\n");
}
// 主函数
int main(void)
{
// 创建定时器1
// 参数说明:
// "timer1": 定时器名称
// timer1out: 定时器超时回调函数
// RT_NULL: 定时器参数
// 1000: 定时器超时时间(单位:毫秒,即1秒)
// RT_TIMER_FLAG_PERIODIC: 定时器为周期触发模式
timer1 = rt_timer_create("timer1", timer1out, RT_NULL, 1000, RT_TIMER_FLAG_PERIODIC);
// 创建定时器2
// 参数说明:
// "timer2": 定时器名称
// timer2out: 定时器超时回调函数
// RT_NULL: 定时器参数
// 3000: 定时器超时时间(单位:毫秒,即3秒)
// RT_TIMER_FLAG_ONE_SHOT: 定时器为单次触发模式
timer2 = rt_timer_create("timer2", timer2out, RT_NULL, 3000, RT_TIMER_FLAG_ONE_SHOT);
// 如果定时器1创建成功,则启动定时器1
if (timer1 != RT_NULL)
{
rt_timer_start(timer1);
}
// 如果定时器2创建成功,则启动定时器2
if (timer2 != RT_NULL)
{
rt_timer_start(timer2);
}
// 主函数返回0,表示正常结束
return 0;
}
实现效果:
