RTThread官網(wǎng)看一下,可以發(fā)現(xiàn)【rt_thread_suspend】的函數(shù)說(shuō)明中,特意的說(shuō)明了這個(gè)函數(shù)不能通過(guò)A線程掛起B(yǎng)線程。

翻開(kāi)源碼看一下,在這個(gè)函數(shù)中有這么一個(gè)判斷,會(huì)判斷需要掛起線程的狀態(tài),如果線程不等于就緒態(tài)那么就不會(huì)進(jìn)入掛起,而sleep,delay等函數(shù)都會(huì)導(dǎo)致線程的掛起,那么其他線程想要掛起這個(gè)線程肯定會(huì)掉到這個(gè)if里面,從而掛起不了這個(gè)線程。

這里有兩個(gè)解決思路,一個(gè)是通過(guò) rt_thread_detach(&thread)刪除線程的方法實(shí)現(xiàn),另外一個(gè)就是定義一個(gè)暫停信號(hào)量,然后在需要暫停的線程中去不停的監(jiān)測(cè)這個(gè)信號(hào)量,當(dāng)接收到信號(hào)量時(shí),自己主動(dòng)掛起線程并讓出CPU,這樣就可以實(shí)現(xiàn)暫停線程,而且還能夠知道線程暫停在哪,下面就第二個(gè)思路進(jìn)行的代碼實(shí)現(xiàn)如下:
#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); //開(kāi)始掃地
}
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); //開(kāi)始洗碗
}
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); //開(kāi)始掃地
}
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;
}
/* 導(dǎo)出到 msh 命令列表中 */
MSH_CMD_EXPORT(pause_thread_init, pause thread);
審核編輯:湯梓紅
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4364瀏覽量
63803 -
線程
+關(guān)注
關(guān)注
0文章
507瀏覽量
20015 -
RTThread
+關(guān)注
關(guān)注
8文章
132瀏覽量
41422
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
RT-Thread記錄(三、RT-Thread線程操作函數(shù))

什么是RT-Thread線程管理看完你就懂了
RT-Thread代碼啟動(dòng)與線程切換過(guò)程的實(shí)現(xiàn)
【原創(chuàng)精選】RT-Thread征文精選技術(shù)文章合集
RT-Thread學(xué)習(xí)筆記 --(6)RT-Thread線程間通信學(xué)習(xí)過(guò)程總結(jié)

RT—thread線程調(diào)度詳解
RT-Thread全球技術(shù)大會(huì):RT-Thread對(duì)POSIX的實(shí)現(xiàn)情況介紹

RT-Thread學(xué)習(xí)筆記 RT-Thread的架構(gòu)概述

RT-Thread v5.0.2 發(fā)布

評(píng)論