RT-Thread 入门学习笔记 - 创建无法删除的线程

发布于 2021-03-12 09:07:08

前言

  • 学习了RT-Thread的内核组件与线程管理,我忽然发现,线程创建时,名字可以使用RT_NULL。
  • 创建无名线程,工作正常,只是,无法删除

相关代码

#include <rtthread.h>

#define DBG_SECTION_NAME    "noname"
#define DBG_LEVEL           DBG_LOG
#include <rtdbg.h>

 /* led test */
extern void led_red_on(void);
extern void led_red_off(void);
extern void led_grn_on(void);
extern void led_grn_off(void);
extern void led_blu_on(void);
extern void led_blu_off(void);

/* no name thread1 */
static void no_name_task1(void* param)
{
    LOG_D("no name thread1 start!\n");

    while (1)
    {
        led_red_on();
        rt_thread_mdelay(2000);
        led_red_off();
        rt_thread_mdelay(2000);
    }
}

/* no name thread2 */
static void no_name_task2(void* param)
{
    LOG_D("no name thread2 start!\n");

    while (1)
    {
        led_blu_on();
        rt_thread_mdelay(3000);
        led_blu_off();
        rt_thread_mdelay(3000);
    }
}

/* no name thread3 */
static void no_name_task3(void* param)
{
    LOG_D("no name thread3 start!\n");

    while (1)
    {
        led_grn_on();
        rt_thread_mdelay(4000);
        led_grn_off();
        rt_thread_mdelay(4000);
    }
}

void no_name_init(void)
{
    rt_thread_t tid;

    tid = rt_thread_create(RT_NULL, no_name_task1, RT_NULL, 2048, 28, 50);
    rt_thread_startup(tid);

    tid = rt_thread_create(RT_NULL, no_name_task2, RT_NULL, 2048, 28, 50);
    rt_thread_startup(tid);

    tid = rt_thread_create(RT_NULL, no_name_task3, RT_NULL, 2048, 28, 50);
    rt_thread_startup(tid);
}

void no_name_del(void)
{
    rt_thread_t tid;

    tid = rt_thread_find(RT_NULL);
    if (tid != RT_NULL)
    {
        rt_thread_delete (tid);
    }
    else
    {
        LOG_D("Not find thread!");
    }
}

MSH_CMD_EXPORT(no_name_init, no_name_init);
MSH_CMD_EXPORT(no_name_del, no_name_del);
msh >no_name_init
msh >[D/noname] no name thread1 start!

[D/noname] no name thread2 start!

[D/noname] no name thread3 start!


msh >list_thread
thread   pri  status      sp     stack size max used left tick  error
-------- ---  ------- ---------- ----------  ------  ---------- ---                                                                               "        28  suspend 0x0000007c 0x00000800    07%   0x0000002e 000                                                                                 "        28  suspend 0x0000007c 0x00000800    07%   0x0000002e 000                                                                                 "        28  suspend 0x0000007c 0x00000800    07%   0x0000002e 000
pms       28  suspend 0x0000009c 0x00000800    07%   0x0000002f 000
tshell    20  running 0x00000084 0x00001000    12%   0x00000005 000
serial    25  suspend 0x00000088 0x00000400    13%   0x0000000a 000
tidle0    31  ready   0x00000080 0x00000800    06%   0x00000016 000
timer      4  suspend 0x00000060 0x00000200    18%   0x00000009 000
msh >

msh >no_name_del
[D/noname] Not find thread!

解读

  • 之所能创建无名的线程,因为内核对象初始化时,并不判断name的长度
  • 之所无法删除没有名字的线程,是因为内核对象的查找,是用名字来查找的。

总结

  • 需要继续研究RT-Thread内核代码、线程管理代码,正确使用RT-Thread
  • 加深基础理论知识的积累。
1 条评论

发布
问题