RTThread官网看一下,可以发现【rt_thread_suspend】的函数说明中,特意的说明了这个函数不能通过A线程挂起B线程。

翻开源码看一下,在这个函数中有这么一个判断,会判断需要挂起线程的状态,如果线程不等于就绪态那么就不会进入挂起,而sleep,delay等函数都会导致线程的挂起,那么其他线程想要挂起这个线程肯定会掉到这个if里面,从而挂起不了这个线程。

这里有两个解决思路,一个是通过 rt_thread_detach(&thread)删除线程的方法实现,另外一个就是定义一个暂停信号量,然后在需要暂停的线程中去不停的监测这个信号量,当接收到信号量时,自己主动挂起线程并让出CPU,这样就可以实现暂停线程,而且还能够知道线程暂停在哪,下面就第二个思路进行的代码实现如下:
#include
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t a_thread_stack[ 512 ];
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t b_thread_stack[ 512 ];
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t c_thread_stack[ 512 ];
static struct rt_thread a_thread;
static struct rt_thread b_thread;
static struct rt_thread c_thread;
static rt_sem_t b_pause_sem = RT_NULL;
static rt_sem_t c_pause_sem = RT_NULL;
static void a_thread_entry(void *parameter)
{
unsigned int count = 0;
while (1)
{
count++;
if(count == 10)
{
rt_kprintf("b start!\n");
rt_thread_startup(&b_thread); //开始扫地
}
else if(count == 30)
{
rt_kprintf("b pause!\n");
rt_sem_release(b_pause_sem);//rt_thread_suspend(&b_thread); //停止扫地
rt_kprintf("c start!\n");
rt_thread_startup(&c_thread); //开始洗碗
}
else if(count == 50)
{
rt_kprintf("c pause!\n");
rt_sem_release(c_pause_sem);//rt_thread_suspend(&c_thread); //停止洗碗
rt_kprintf("b start!\n");
rt_thread_resume(&b_thread); //开始扫地
}
rt_thread_delay(100);
}
}
static void b_thread_entry(void *parameter)
{
unsigned int count = 0;
while (1)
{
rt_kprintf("b thread run!\n");
rt_thread_delay(300);
if(rt_sem_take(b_pause_sem, 0) == RT_EOK)
{
rt_thread_suspend(&b_thread); //停止扫地
rt_schedule();
}
}
}
static void c_thread_entry(void *parameter)
{
unsigned int count = 0;
while (1)
{
rt_kprintf("c thread run!\n");
rt_thread_delay(100);
if(rt_sem_take(c_pause_sem, 0) == RT_EOK)
{
rt_thread_suspend(&c_thread); //停止扫地
rt_schedule();
}
}
}
int pause_thread_init(void)
{
rt_err_t result;
b_pause_sem = rt_sem_create("b_pause", 0, RT_IPC_FLAG_PRIO);
c_pause_sem = rt_sem_create("c_pause", 0, RT_IPC_FLAG_PRIO);
/* init led thread */
result = rt_thread_init(&a_thread,
"a_thread",
a_thread_entry,
RT_NULL,
(rt_uint8_t *)&a_thread_stack[0],
sizeof(a_thread_stack),
5,
5);
if (result == RT_EOK)
{
rt_thread_startup(&a_thread);
}
/* init led thread */
result = rt_thread_init(&b_thread,
"b_thread",
b_thread_entry,
RT_NULL,
(rt_uint8_t *)&b_thread_stack[0],
sizeof(b_thread_stack),
6,
5);
/* init led thread */
result = rt_thread_init(&c_thread,
"c_thread",
c_thread_entry,
RT_NULL,
(rt_uint8_t *)&c_thread_stack[0],
sizeof(c_thread_stack),
7,
5);
return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(pause_thread_init, pause thread);
审核编辑:汤梓红
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。
举报投诉
-
函数
+关注
关注
3文章
4353浏览量
63292 -
线程
+关注
关注
0文章
507浏览量
19855 -
RTThread
+关注
关注
8文章
132浏览量
41141
发布评论请先 登录
相关推荐
RT-Thread代码启动与线程切换过程的实现
的,换一下函数名就好了2、RT-Thread线程切换过程首先查看RT-Thread内核架构这一章节,明白RT-Thread链表及
发表于 04-25 11:38
【原创精选】RT-Thread征文精选技术文章合集
开发板的详细步骤例程stm32裸机RT—thread开始创建线程详解基于标准库的keil移植到RT-thread例程RT-thread
发表于 07-26 14:56
RT-Thread学习笔记 --(6)RT-Thread线程间通信学习过程总结
前两篇文章总结了RT-Thread多线程以及多线程同步的学习过程,关于前两篇学习总结,可以查看之前的文章。
发表于 01-25 18:50
•7次下载

RT—thread线程调度详解
系统调度就是在就绪列表中寻找优先级最高的就绪线程,然后去执行该线程。但是目前我们还不支持优先级, 仅实现两个线程轮流切换,系统调度函数
RT-Thread v5.0.2 发布
RT-Thread 代码仓库地址: ● https://github.com/RT-Thread/rt-thread RT-Thread 5.0.2 版本发布日志详情: ● htt

评论