RT-Thread 学习第一天 创建静动态线程 并删除线程

使用RT-Thread  Keil MDK模拟器STM32F103体验工程模拟运行。

rt_err_t  result;

void User_Thread_One(void)
{
	unsigned int i=0;
	while(1)
	{
			rt_kprintf("******Thread_One_Static****** %d\n",i++);
			rt_thread_mdelay(250);
	}
}
#define THREAD_PRIORITY         25
#define THREAD_STACK_SIZE       512
#define THREAD_TIMESLICE        5

static rt_thread_t thread2 = RT_NULL;
void User_Thread_Two(void)
{
	unsigned int cout = 0;
	while(1)
	{
		rt_kprintf("User_Thread_Two_Dynamic  %d\n",cout++);
		rt_thread_mdelay(250);
	}
}
static char thread1_stack[1024];
static struct rt_thread thread1;
//创建两个线程 线程1 为静态  线程2 为动态
int  User_Init(void)
{

	rt_thread_init(&thread1,"User_Thread_One",User_Thread_One,RT_NULL,&thread1_stack[0],sizeof(thread1_stack),THREAD_PRIORITY-1,THREAD_TIMESLICE);
	rt_thread_startup(&thread1);
	

	thread2 = rt_thread_create("User_Thread_Two",User_Thread_Two,RT_NULL,THREAD_STACK_SIZE,THREAD_PRIORITY,THREAD_TIMESLICE);
	if(thread2 != RT_NULL) rt_thread_startup(thread2);
	return 0;
}
//导出到msh命令提示列表
MSH_CMD_EXPORT(User_Init, User Thread Init );
//删除两个线程
int user_kill(void)
{
	result = rt_thread_detach(&thread1);		//  删除静态线程
	if(result == RT_EOK)
		rt_kprintf("Thread_One Delete OK\n");
	else
		rt_kprintf("Thread_One Delete Error\n");
	
	
	result = rt_thread_delete(thread2);		//删除动态线程
	if(result == RT_EOK)
		rt_kprintf("Thread_Two Delete OK\n");
	else
		rt_kprintf("Thread_Two Delete Error\n");
	return 0;
}
//导出到msh命令提示列表
MSH_CMD_EXPORT(user_kill, User Thread kill );

运行结果:

RT-Thread 学习第一天 创建静动态线程 并删除线程_第1张图片

RT-Thread 学习第一天 创建静动态线程 并删除线程_第2张图片

你可能感兴趣的:(RT-Thread 学习第一天 创建静动态线程 并删除线程)