/* 静态线程的 线程堆栈*/ static rt_uint8_t thread1_stack[512]; static rt_uint8_t thread2_stack[512]; /* 静态线程的 线程控制块 */ static struct rt_thread thread_test1; static struct rt_thread thread_test2; static void test1_thread_entry(void* parameter); static void test2_thread_entry(void* parameter); void demo_thread_creat(void) { rt_err_t result; /* 创建静态线程 : 优先级 15 ,时间片 2个系统滴答 */ result = rt_thread_init(&thread_test1, "test1", test1_thread_entry, RT_NULL, (rt_uint8_t*)&thread1_stack[0], sizeof(thread1_stack), 15, 2); if (result == RT_EOK) { rt_thread_startup(&thread_test1); } /* 创建静态线程 : 优先级 15 ,时间片 1个系统滴答 */ result = rt_thread_init(&thread_test2, "test2", test2_thread_entry, RT_NULL, (rt_uint8_t*)&thread2_stack[0], sizeof(thread2_stack), 15, 1); if (result == RT_EOK) { rt_thread_startup(&thread_test2); } } void test1_thread_entry(void* parameter) { rt_uint8_t i; for(i = 0; i < 6; i ++) { rt_kprintf("Thread1:\r\n"); rt_kprintf("This is a demo for same priority !\r\n"); rt_thread_delay(4); } } void test2_thread_entry(void* parameter) { rt_uint8_t j; for(j = 0; j <60; j ++) { rt_kprintf("Thread2:\r\n"); rt_kprintf("This is a demo for same priority !\r\n"); } }
线程 test2 的打印信息输出不全,说明 test2 线程的执行被打断了, 因为 test1 线程和 test2线程的优先级都是 15,并不会发生抢占的情况,所以说 test2 线程是等到自己的执行时间片到达时,被系统剥夺了 CPU 使用权,而将使用权交给了 test1 线程,从而 test1 线程重新获得执行.由此可以看出当两个相同线程间,运行是以时间片为基准的,时间片到达,则交出CPU 使用权,交给下一个就绪的同优先级线程执行。
PS:由于上面的两个线程都不是无限循环结构,在其正常退出后,其线程状态变为初始化状态,然后在空闲线程中将其从线程调度列表中删除。学到这里可以再创建个任务,一段时间以后调用rt_thread_startup再变成就绪状态。代码如下:
#include <rtthread.h> #include <stm32f10x.h> #include "test.h" /* 变量分配4字节对齐 */ ALIGN(RT_ALIGN_SIZE) /* 静态线程的 线程堆栈*/ static rt_uint8_t thread1_stack[512]; static rt_uint8_t thread2_stack[512]; static rt_uint8_t thread3_stack[512]; /* 静态线程的 线程控制块 */ static struct rt_thread thread_test1; static struct rt_thread thread_test2; static struct rt_thread thread_test3; static void test1_thread_entry(void* parameter); static void test2_thread_entry(void* parameter); static void test3_thread_entry(void* parameter); static rt_uint8_t test3_thread_flag = 0; void demo_thread_creat(void) { rt_err_t result; /* 创建静态线程 : 优先级 15 ,时间片 2个系统滴答 */ result = rt_thread_init(&thread_test1, "test1", test1_thread_entry, RT_NULL, (rt_uint8_t*)&thread1_stack[0], sizeof(thread1_stack), 15, 2); if (result == RT_EOK) { rt_thread_startup(&thread_test1); } /* 创建静态线程 : 优先级 15 ,时间片 1个系统滴答 */ result = rt_thread_init(&thread_test2, "test2", test2_thread_entry, RT_NULL, (rt_uint8_t*)&thread2_stack[0], sizeof(thread2_stack), 15, 1); if (result == RT_EOK) { rt_thread_startup(&thread_test2); } /* 创建静态线程 : 优先级 14 ,时间片 1个系统滴答 */ result = rt_thread_init(&thread_test3, "test3", test3_thread_entry, RT_NULL, (rt_uint8_t*)&thread3_stack[0], sizeof(thread3_stack), 14, 1); if (result == RT_EOK) { rt_thread_startup(&thread_test3); } } void test1_thread_entry(void* parameter) { rt_uint8_t i; for(i = 0; i < 6; i ++) { rt_kprintf("Thread1:\r\n"); rt_kprintf("This is a demo for same priority !\r\n"); rt_thread_delay(4); } } void test2_thread_entry(void* parameter) { rt_uint8_t j; for(j = 0; j <60; j ++) { rt_kprintf("Thread2:\r\n"); rt_kprintf("This is a demo for same priority !\r\n"); } } void test3_thread_entry(void* parameter) { while(1) { rt_thread_delay(3000); if(0 == test3_thread_flag) { rt_thread_startup(&thread_test1); rt_thread_startup(&thread_test2); test3_thread_flag = 1; } } }
本例程同样需要关闭 finsh 组件。