1. 程式人生 > >[RTT例程練習] 1.1 動態執行緒建立,刪除

[RTT例程練習] 1.1 動態執行緒建立,刪除

建立兩個動態執行緒,thread2 執行4s後刪除thread1。

這裡兩個都為動態執行緒,所謂動態執行緒即在堆中動態建立,刪除之後也從RAM中消失。區別於靜態執行緒。

由於是動態,所以需開啟

#define RT_USING_HEAP

以下是application.c 的程式碼
#include <rtthread.h>

rt_thread_t tid1 = RT_NULL;
rt_thread_t tid2 = RT_NULL;

static void thread1_entry(void* parameter)
{
    rt_uint32_t count = 0;
    rt_kprintf("thread1 dynamicly created ok\n");
    while(1)
    {
        rt_kprintf("thread1 count: %d\n",count++);
        rt_thread_delay(RT_TICK_PER_SECOND);
    }
}

static void thread2_entry(void* parameter)
{
    rt_kprintf("thread2 dynamicly created ok\n");

    rt_thread_delay(RT_TICK_PER_SECOND * 4);
    
    rt_thread_delete(tid1);
    rt_kprintf("thread1 deleted ok\n");
}

int rt_application_init()
{
//    rt_thread_t init_thread;
//    
//    rt_err_t result;
//
//    if (init_thread != RT_NULL)
//        rt_thread_startup(init_thread);

    tid1 = rt_thread_create("thread1",
        thread1_entry, 
        RT_NULL,
        512, 6, 10);
    if (tid1 != RT_NULL)
        rt_thread_startup(tid1);

    tid2 = rt_thread_create("thread2",
        thread2_entry, RT_NULL,
        512, 6, 10);
    if (tid2 != RT_NULL)
        rt_thread_startup(tid2);

    return 0;
}


/*@}*/

輸出結果為:
\ | /
- RT - Thread Operating System
/ | \ 1.1.0 build Aug 10 2012
2006 - 2012 Copyright by rt-thread team
thread1 dynamicly created ok
thread1 count: 0
thread2 dynamicly created ok
thread1 count: 1
thread1 count: 2
thread1 count: 3
thread1 count: 4
thread1 deleted ok