3.RT-Thread线程的创建与删除,动态线程、静态进程

在实际应用中,经常添加多个 .c 文件和 .h 文件,RT-Thread借助自动构建系统--Scon,它会自动添加你的.c和.h文件到你的工程中,Scon工具根据package-kernel目录下的SConscripts构建工程。  

添加RTT_ROOT(rtt的源码目录)环境变量然后在命令行中构建。

线程的优先级数字越大,优先级越小,如果线程优先级一样,则采取时间片轮询方式。

rt_thread_create     创建动态线程

rt_thread_delete     删除动态线程     静态线程和动态线程的区别就在线程堆栈的分配方式不一样。

rt_thread_init          创建静态线程 ,事先定义一个数组,把数组地址给堆栈起始地址

rt_thread_detach    删除静态线程      

rt_thread_startup     启动线程

rt_thread_cleanup      回收线程(可选)


线程的组成:

A    线程代码

    1. 无线循环模式,代码中有while(1){},一直执行某个功能,但是在while(1)中需要采取方式让出CPU使用权,比如rt_thread_delay(),

    2. 顺序执行模式,执行完之后线程退出,线程资源被回收。

B    线程控制块 tid1=rt_thread_creat(), 

        动态线程控制块static struct rt_thread_t tid1;

        静态线程控制块 static struct rt_thread tid1;

C    线程堆栈,可以在shell中执行list_thread来观察线程所需最大堆栈。


/*
 * 程序清单:创建和删除线程例程
 *
 * 这个例子会创建两个线程,在一个线程中删除另外一个线程。
 */
#include 

#define THREAD_PRIORITY     25
#define THREAD_STACK_SIZE   512
#define THREAD_TIMESLICE    5

/*
 * 线程删除(rt_thread_delete)函数仅适合于动态线程,为了在一个线程
 * 中访问另一个线程的控制块,所以把线程块指针声明成全局类型以供全
 * 局访问
 */
static rt_thread_t tid1 = RT_NULL;//动态创建线程控制块
static struct rt_thread tid2;//静态创建线程控制块

static rt_uint8_t thread2_stack[THREAD_STACK_SIZE];//静态堆栈

/* 线程1的入口函数 */
static void thread1_entry(void *parameter)
{
    rt_uint32_t count = 0;

    while (1)
    {
        /* 线程1采用低优先级运行,一直打印计数值 */
        rt_kprintf("thread count: %d\n", count ++);
        count ++;
    }
}

static void thread1_cleanup(struct rt_thread *tid)
{
    if (tid != tid1)
    {
        return ;
    }
    rt_kprintf("thread1 end\n");
    tid1 = RT_NULL;
}

/* 线程2的入口函数 */
static void thread2_entry(void *parameter)
{
    /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */

    /* 线程2启动后先睡眠10个OS Tick */
    rt_thread_delay(10);

    /*
     * 线程2唤醒后直接删除线程1,删除线程1后,线程1自动脱离就绪线程
     * 队列
     */
    rt_thread_delete(tid1);

    /*
     * 线程2继续休眠10个OS Tick然后退出,线程2休眠后应切换到idle线程
     * idle线程将执行真正的线程1控制块和线程栈的删除
     */
    rt_thread_delay(10);
}

static void thread2_cleanup(struct rt_thread *tid)
{
    /*
     * 线程2运行结束后也将自动被删除(线程控制块和线程栈在idle线
     * 程中释放)
     */

    if (tid != &tid2)
    {
        return ;
    }
    rt_kprintf("thread2 end\n");
    
}

/* 线程示例的初始化 */
int thread_sample_init()
{
    /* 创建线程1 */
    tid1 = rt_thread_create("t1", /* 线程1的名称是t1 */
                            thread1_entry, RT_NULL,   /* 入口是thread1_entry,参数是RT_NULL */
                            THREAD_STACK_SIZE, THREAD_PRIORITY, THREAD_TIMESLICE);
    if (tid1 != RT_NULL) /* 如果获得线程控制块,启动这个线程 */
    {
        tid1->cleanup = thread1_cleanup;
        rt_thread_startup(tid1);
    }

    /* 创建线程2 */
    rt_err_t res;
		res=rt_thread_init(&tid2,
                        "tid2",
                        thread2_entry,
                        RT_NULL,
                        thread2_stack,
                        THREAD_STACK_SIZE,
                        THREAD_PRIORITY,
                        THREAD_TIMESLICE);
    if (res == RT_EOK) /* 如果获得线程控制块,启动这个线程 */
    {
        tid2.cleanup = thread2_cleanup;
        rt_thread_startup(&tid2);
    }

    return 0;
}
/* 如果设置了RT_SAMPLES_AUTORUN,则加入到初始化线程中自动运行 */
#if defined (RT_SAMPLES_AUTORUN) && defined(RT_USING_COMPONENTS_INIT)
    INIT_APP_EXPORT(thread_sample_init);
#endif
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(thread_sample_init, run signal sample);


你可能感兴趣的:(RT-Thread学习笔记)