[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