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

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

這里有兩個解決思路,一個是通過 rt_thread_detach(&thread)刪除線程的方法實(shí)現(xiàn),另外一個就是定義一個暫停信號量,然后在需要暫停的線程中去不停的監(jiān)測這個信號量,當(dāng)接收到信號量時,自己主動掛起線程并讓出CPU,這樣就可以實(shí)現(xiàn)暫停線程,而且還能夠知道線程暫停在哪,下面就第二個思路進(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); //開始掃地
}
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;
}
/* 導(dǎo)出到 msh 命令列表中 */
MSH_CMD_EXPORT(pause_thread_init, pause thread);
審核編輯:湯梓紅
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報(bào)投訴
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4413瀏覽量
67194 -
線程
+關(guān)注
關(guān)注
0文章
508瀏覽量
20794 -
RTThread
+關(guān)注
關(guān)注
8文章
132瀏覽量
42634
發(fā)布評論請先 登錄
相關(guān)推薦
熱點(diǎn)推薦
RT-Thread記錄(三、RT-Thread線程操作函數(shù))
講完了RT-Thread開發(fā)環(huán)境,啟動流程,啟動以后當(dāng)然是開始跑線程了,那么自然我們得學(xué)會如何創(chuàng)建線程以及線程的有關(guān)操作。
深度剖析 RT-Thread 線程調(diào)度流程
RT-Thread調(diào)度第一個線程的主要流程分如下:rtthread_startup:RTT的啟動函數(shù),主要負(fù)責(zé)板級驅(qū)動,調(diào)度器,系統(tǒng)線程初始化,啟動調(diào)度的工作
什么是RT-Thread線程管理看完你就懂了
可由用戶掛接一些數(shù)據(jù)信息到線程控制塊中,以提供類似線程私有數(shù)據(jù)的實(shí)現(xiàn)。線程重要屬性線程棧RT-Thre
發(fā)表于 03-29 06:16
RT-Thread代碼啟動與線程切換過程的實(shí)現(xiàn)
的,換一下函數(shù)名就好了2、RT-Thread線程切換過程首先查看RT-Thread內(nèi)核架構(gòu)這一章節(jié),明白RT-Thread鏈表及
發(fā)表于 04-25 11:38
【原創(chuàng)精選】RT-Thread征文精選技術(shù)文章合集
開發(fā)板的詳細(xì)步驟例程stm32裸機(jī)RT—thread開始創(chuàng)建線程詳解基于標(biāo)準(zhǔn)庫的keil移植到RT-thread例程RT-thread
發(fā)表于 07-26 14:56
RT-Thread學(xué)習(xí)筆記 --(6)RT-Thread線程間通信學(xué)習(xí)過程總結(jié)
前兩篇文章總結(jié)了RT-Thread多線程以及多線程同步的學(xué)習(xí)過程,關(guān)于前兩篇學(xué)習(xí)總結(jié),可以查看之前的文章。
發(fā)表于 01-25 18:50
?7次下載
RT—thread線程調(diào)度詳解
系統(tǒng)調(diào)度就是在就緒列表中尋找優(yōu)先級最高的就緒線程,然后去執(zhí)行該線程。但是目前我們還不支持優(yōu)先級, 僅實(shí)現(xiàn)兩個線程輪流切換,系統(tǒng)調(diào)度函數(shù)
RT-Thread全球技術(shù)大會:RT-Thread對POSIX的實(shí)現(xiàn)情況介紹
RT-Thread全球技術(shù)大會:RT-Thread對POSIX的實(shí)現(xiàn)情況介紹 ? ? ? ? ? ? 審核編輯:彭靜
RT-Thread學(xué)習(xí)筆記 RT-Thread的架構(gòu)概述
聯(lián)網(wǎng)操作系統(tǒng)。 RT-Thread 概述 RT-Thread,全稱是 Real Time-Thread,顧名思義,它是一個嵌入式實(shí)時多線程
RT-thread線程切換原理與實(shí)現(xiàn)
評論