RT-Thread相同优先级线程的调度

时间:2022-08-23 05:30:48
/*  静态线程的 线程堆栈*/
static rt_uint8_t thread1_stack[];
static rt_uint8_t thread2_stack[]; /* 静态线程的 线程控制块 */
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[], sizeof(thread1_stack), , ); 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[], sizeof(thread2_stack), , ); if (result == RT_EOK)
{
rt_thread_startup(&thread_test2);
} } void test1_thread_entry(void* parameter)
{
rt_uint8_t i; for(i = ; i < ; i ++)
{
rt_kprintf("Thread1:\r\n");
rt_kprintf("This is a demo for same priority !\r\n");
rt_thread_delay();
}
} void test2_thread_entry(void* parameter)
{
rt_uint8_t j; for(j = ; j <; j ++)
{
rt_kprintf("Thread2:\r\n");
rt_kprintf("This is a demo for same priority !\r\n");
}
}

RT-Thread相同优先级线程的调度

线程 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[];
static rt_uint8_t thread2_stack[];
static rt_uint8_t thread3_stack[]; /* 静态线程的 线程控制块 */
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 = ; 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[], sizeof(thread1_stack), , ); 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[], sizeof(thread2_stack), , ); 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[], sizeof(thread3_stack), , ); if (result == RT_EOK)
{
rt_thread_startup(&thread_test3);
} } void test1_thread_entry(void* parameter)
{
rt_uint8_t i; for(i = ; i < ; i ++)
{
rt_kprintf("Thread1:\r\n");
rt_kprintf("This is a demo for same priority !\r\n");
rt_thread_delay();
}
} void test2_thread_entry(void* parameter)
{
rt_uint8_t j; for(j = ; j <; j ++)
{
rt_kprintf("Thread2:\r\n");
rt_kprintf("This is a demo for same priority !\r\n");
}
} void test3_thread_entry(void* parameter)
{
while()
{
rt_thread_delay();
if( == test3_thread_flag)
{
rt_thread_startup(&thread_test1);
rt_thread_startup(&thread_test2);
test3_thread_flag = ;
}
}
}

本例程同样需要关闭 finsh 组件。