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);
审核编辑:汤梓红
全部0条评论
快来发表一下你的评论吧 !