当前位置: 首页 > news >正文

RT-Thread之创建线程

一、静态线程创建

1、thread_task.c文件

#include "thread_task.h"
#include "main.h"
#include <stdio.h>      
#include "rtthread.h"/******************************************** 线程 1 ******************************************************/
#define THREAD_1_PRIORITY  		5           /* 进程优先级 */
#define THREAD_1_STACK_SIZE		512         /* 进程栈空间大小 */
#define THREAD_1_TIMESLICE		1           /* 进程执行时间片个数 */
static char thread_1_stack[THREAD_1_STACK_SIZE];   /* 进程栈 */
static struct rt_thread thread_1_handle;        /* 进程句柄 *//******************************************** 线程 2 ******************************************************/
#define THREAD_2_PRIORITY  		5           /* 进程优先级 */
#define THREAD_2_STACK_SIZE		512         /* 进程栈空间大小 */
#define THREAD_2_TIMESLICE		1           /* 进程执行时间片个数 */
static char thread_2_stack[THREAD_1_STACK_SIZE];    /* 进程栈 */
static struct rt_thread thread_2_handle;    /* 进程句柄 */uint8_t count_1 = 0;
uint8_t count_2 = 0;/*** @brief   线程1入口函数* @param   无* @retval  无*/
void thread_1_entry(void* param)
{while(1){rt_tick_t now_tick = rt_tick_get();     /* 获取当前时间 */HAL_GPIO_TogglePin(GPIOC, LED1_Pin);rt_thread_delay_until(&now_tick, 1000);     /* 精准延时1000时间片 */}
}/*** @brief   线程2入口函数* @param   无* @retval  无*/
void thread_2_entry(void* param)
{while(1){rt_tick_t now_tick = rt_tick_get();     /* 获取当前时间 */HAL_GPIO_TogglePin(GPIOC, LED2_Pin);rt_thread_delay_until(&now_tick, 1000);     /* 精准延时1000时间片 */}}/*** @brief   创建线程任务并启动* @param   无* @retval  无*/
void ThreadStart(void)
{/* 初始化静态线程*/rt_thread_init(&thread_1_handle,		/* 线程句柄*/"thread_1",				/* 线程句柄名称*/thread_1_entry,			/* 函数入口 */RT_NULL,				/* 入口函数参数 */&thread_1_stack[0],		/* 线程栈地址 */THREAD_1_STACK_SIZE,	/* 线程栈大小 */THREAD_1_PRIORITY,		/* 线程优先级 */THREAD_1_TIMESLICE);	/* 线程时间片大小 */rt_thread_startup(&thread_1_handle); /* 启动线程 *//* 初始化静态线程*/rt_thread_init(&thread_2_handle,		/* 线程句柄*/"thread_2",				/* 线程句柄名称*/thread_2_entry,			/* 函数入口 */RT_NULL,				/* 入口函数参数 */&thread_2_stack[0],		/* 线程栈地址 */THREAD_2_STACK_SIZE,	/* 线程栈大小 */THREAD_2_PRIORITY,		/* 线程优先级 */THREAD_2_TIMESLICE);	/* 线程时间片大小 */rt_thread_startup(&thread_2_handle); /* 启动线程 */
}

2、thread_task.h文件

#ifndef __THREAD_TASK_H
#define __THREAD_TASK_H#ifdef __cplusplus
extern "C" {
#endifvoid ThreadStart(void);#ifdef __cplusplus
}
#endif#endif /* __THREAD_TASK_H */

二、静态创建线程(自定义实现调度)

1、thread_task.c文件

#include "thread_task.h"
#include "main.h"
#include <stdio.h>      
#include "rtthread.h"/******************************************** 线程 1 ******************************************************/
#define THREAD_1_CYCLE          1000        /* 进程执行周期 */
#define THREAD_1_PRIORITY  		5           /* 进程优先级 */
#define THREAD_1_STACK_SIZE		512         /* 进程栈空间大小 */
#define THREAD_1_TIMESLICE		1           /* 进程执行时间片个数 */
static char thread_1_stack[THREAD_1_STACK_SIZE];   /* 进程栈 */
static struct rt_thread thread_1_handle;        /* 进程句柄 *//******************************************** 线程 2 ******************************************************/
#define THREAD_2_CYCLE          1000        /* 进程执行周期 */
#define THREAD_2_PRIORITY  		5           /* 进程优先级 */
#define THREAD_2_STACK_SIZE		512         /* 进程栈空间大小 */
#define THREAD_2_TIMESLICE		1           /* 进程执行时间片个数 */
static char thread_2_stack[THREAD_1_STACK_SIZE];    /* 进程栈 */
static struct rt_thread thread_2_handle;    /* 进程句柄 */uint8_t count_1 = 0;
uint8_t count_2 = 0;/*** @brief   线程1入口函数* @param   无* @retval  无*/
void thread_1_entry(void* param)
{while(1){HAL_GPIO_TogglePin(GPIOC, LED1_Pin);rt_thread_suspend(&thread_1_handle);  //挂起线程rt_schedule();              //启动调度器}
}/*** @brief   线程2入口函数* @param   无* @retval  无*/
void thread_2_entry(void* param)
{while(1){HAL_GPIO_TogglePin(GPIOC, LED2_Pin);rt_thread_suspend(&thread_2_handle);  //挂起线程rt_schedule();              //启动调度器}}/*** @brief   创建线程任务并启动* @param   无* @retval  无*/
void ThreadStart(void)
{/* 初始化静态线程*/rt_thread_init(&thread_1_handle,		/* 线程句柄*/"thread_1",				/* 线程句柄名称*/thread_1_entry,			/* 函数入口 */RT_NULL,				/* 入口函数参数 */&thread_1_stack[0],		/* 线程栈其实地址 */THREAD_1_STACK_SIZE,	/* 线程栈大小 */THREAD_1_PRIORITY,		/* 线程优先级 */THREAD_1_TIMESLICE);	/* 线程时间片大小 */rt_thread_startup(&thread_1_handle); /* 启动线程 *//* 初始化静态线程*/rt_thread_init(&thread_2_handle,		/* 线程句柄*/"thread_2",				/* 线程句柄名称*/thread_2_entry,			/* 函数入口 */RT_NULL,				/* 入口函数参数 */&thread_2_stack[0],		/* 线程栈其实地址 */THREAD_2_STACK_SIZE,	/* 线程栈大小 */THREAD_2_PRIORITY,		/* 线程优先级 */THREAD_2_TIMESLICE);	/* 线程时间片大小 */rt_thread_startup(&thread_2_handle); /* 启动线程 */
}/*任务调度*/
void Task_Schedule(void)
{static uint32_t task_count = 0;if(task_count%1000 == 0)   rt_thread_resume(&thread_1_handle);if(task_count%2000 == 0)   rt_thread_resume(&thread_2_handle);rt_schedule();task_count++;//计数值达到一定值后清零,防止任务调度错乱if(task_count == 200000)	task_count = 0; 
}

2、thread_task.h文件

#ifndef __THREAD_TASK_H
#define __THREAD_TASK_H#ifdef __cplusplus
extern "C" {
#endifvoid ThreadStart(void);
void Task_Schedule(void);#ifdef __cplusplus
}
#endif#endif /* __THREAD_TASK_H */

2、修改board.c中的SysTick_Handler()函数

extern void Task_Schedule(void);void SysTick_Handler(void)
{/* 进入中断 */rt_interrupt_enter();/* 更新时基 */rt_tick_increase();/* 自定义调度函数 */Task_Schedule();/* 离开中断 */rt_interrupt_leave();
}

三、动态创建线程

1、thread_task.c文件

#include "thread_task.h"
#include "main.h"
#include <stdio.h>      
#include "rtthread.h"/******************************************** 线程 1 ******************************************************/
#define THREAD_1_PRIORITY  		5           /* 进程优先级 */
#define THREAD_1_STACK_SIZE		512         /* 进程栈空间大小 */
#define THREAD_1_TIMESLICE		1           /* 进程执行时间片个数 */
static struct rt_thread *thread_1_handle;        /* 进程句柄 *//******************************************** 线程 2 ******************************************************/
#define THREAD_2_PRIORITY  		5           /* 进程优先级 */
#define THREAD_2_STACK_SIZE		512         /* 进程栈空间大小 */
#define THREAD_2_TIMESLICE		1           /* 进程执行时间片个数 */
static struct rt_thread *thread_2_handle;    /* 进程句柄 *//*** @brief   线程1入口函数* @param   无* @retval  无*/
void thread_1_entry(void* param)
{while(1){rt_tick_t now_tick = rt_tick_get();     /* 获取当前时间 */HAL_GPIO_TogglePin(GPIOC, LED1_Pin);rt_thread_delay_until(&now_tick, 1000);     /* 精准延时1000时间片 */}
}/*** @brief   线程2入口函数* @param   无* @retval  无*/
void thread_2_entry(void* param)
{while(1){rt_tick_t now_tick = rt_tick_get();     /* 获取当前时间 */HAL_GPIO_TogglePin(GPIOC, LED2_Pin);rt_thread_delay_until(&now_tick, 1000);     /* 精准延时1000时间片 */}}/*** @brief   动态创建线程任务并启动* @param   无* @retval  无*/
void ThreadStart(void)
{/* 动态创建线程 */thread_1_handle = rt_thread_create("thread_1",				/* 线程句柄名称*/thread_1_entry,			/* 函数入口 */RT_NULL,				/* 入口函数参数 */THREAD_1_STACK_SIZE,	/* 线程栈大小 */THREAD_1_PRIORITY,		/* 线程优先级 */THREAD_1_TIMESLICE  	/* 线程时间片大小 */);rt_thread_startup(thread_1_handle); /* 启动线程 */thread_2_handle = rt_thread_create("thread_2",				/* 线程句柄名称*/thread_2_entry,			/* 函数入口 */RT_NULL,				/* 入口函数参数 */THREAD_2_STACK_SIZE,	/* 线程栈大小 */THREAD_2_PRIORITY,		/* 线程优先级 */THREAD_2_TIMESLICE  	/* 线程时间片大小 */);rt_thread_startup(thread_2_handle); /* 启动线程 */
}

2、thread_task.h文件

#ifndef __THREAD_TASK_H
#define __THREAD_TASK_H#ifdef __cplusplus
extern "C" {
#endifvoid ThreadStart(void);
void Task_Schedule(void);#ifdef __cplusplus
}
#endif#endif /* __THREAD_TASK_H */

欢迎关注个人微信公众号:比特向阳;
您也可以通过邮箱 lsk0358@163.com与我进行交流;

http://www.hskmm.com/?act=detail&tid=39714

相关文章:

  • cias_voice_plyer_handle.c 解析
  • 【CI130x 离在线】FreeRTOS的流缓冲(StreamBuffer)
  • 数据采集与融合技术作业1
  • RT-Thread Nano源码浅析
  • 关于SQLite - 世界上装机量最多的数据库
  • 《从 “被动听” 到 “主动学”:课堂听讲助力大学生思维成长》
  • 用AI批量生成产品视频!Python+Google Veo 3.1 API让电商转化率飙升
  • 模拟IIC与硬件IIIC哪个更常用?
  • 每日反思(2025_10_26)
  • 251019 NOIP 模拟赛 T2 | dp 及其优化、调整法最优解性质、数形结合
  • 小作业 14(2018 北京高考文科)
  • 10.23总结
  • 10.24总结
  • 第六章习题
  • 速通 花卉鉴赏 短文
  • Agent常见模式 - 智慧园区
  • 概率论测试
  • 2025.10.26总结
  • AI元人文:从战略能力到价值对话的实现框架
  • react-router7.9.4使用
  • Python---开发桌面应用程序
  • Python实现验证码识别的完整流程解析
  • 207. 课程表
  • 基于Python的验证码自动识别方案设计与实现
  • 记录一下
  • 中科大「数学分析教程——上册」习题选做 - Neuro
  • 回忆录:梦开始的往事
  • 20232418 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 大学生为啥一定要认真听讲
  • ti2