RT_Thread 线程时间片轮转

创建两个线程,堆和优先级相同,线程1 时间片为10 、线程2时间片为5。

注意: 时间片轮转机制,在 OS Tick 到来时,正在运行的线程时间片减 1

由于在线程时间片 到来的最后一个OS Tick 时刻,会由操作系统调度进行线程的切换操作。

创建线程 thread1 和 thread2,优先级相同为 20, thread1 时间片为 10, thread2
时间片为 5;
(2)启动线程 thread1 和 thread2,使 thread1 和 thread2 处于就绪状态;
(3)在操作系统的调度下, thread1 首先被投入运行;
(4) thread1 循环打印带有累计计数的信息,当 thread1 运行到第 10 个时间片时,操作系统调度
thread2 投入运行, thread1 进入就绪状态;
(5) thread2 开始运行后,循环打印带有累计计数的信息,直到第 15 个 OS Tick 到来, thread2 已经
运行了 5 个时间片,操作系统调度 thread1 投入运行, thread2 进入就绪状态;
(6) thread1 运行直到计数值 count>200,线程 thread1 退出,接着调度 thread2 运行直到计数值
count>200, thread2 线程退出;之后操作统调度空闲线程投入运行

#define THREAD_STACK_SIZE	1024
#define THREAD_PRIORITY	    20
#define THREAD_TIMESLICE    10

/* 线程入口 */
static void thread_entry(void* parameter)
{
    rt_uint32_t value;
    rt_uint32_t count = 0;

    value = (rt_uint32_t)parameter;
    while (1)
    {
        if(0 == (count % 5))
        {           
            rt_kprintf("thread %d is running ,thread %d count = %d\n", value , value , count);      

            if(count > 200)
                return;            
        }
         count++;
     }  
}

int timeslice_sample(void)
{
    rt_thread_t tid;
    /* 创建线程1 */
    tid = rt_thread_create("thread1", 
                            thread_entry, (void*)1, 
                            THREAD_STACK_SIZE, 
                            THREAD_PRIORITY, THREAD_TIMESLICE); 
    if (tid != RT_NULL) 
        rt_thread_startup(tid);


    /* 创建线程2 */
    tid = rt_thread_create("thread2", 
                            thread_entry, (void*)2,
                            THREAD_STACK_SIZE, 
                            THREAD_PRIORITY, THREAD_TIMESLICE-5);
    if (tid != RT_NULL) 
        rt_thread_startup(tid);
    return 0;
}

/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(timeslice_sample, timeslice sample);

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Rt_thread是一个实时操作系统(RTOS)中的线程管理模块,常用于嵌入式系统开发。下面是一些常用的Rt_thread函数解析: 1. rt_thread_t rt_thread_create(rt_thread_entry_t entry, void *parameter, const char *name, rt_uint8_t stack_size, rt_uint8_t priority, rt_uint32_t tick): 该函数用于创建一个新的线程。参数包括线程入口函数(entry)、传递给线程的参数(parameter)、线程名称(name)、线程堆栈大小(stack_size)、线程优先级(priority)和时间片轮转周期(tick)。 2. void rt_thread_startup(rt_thread_t thread): 该函数用于启动一个已创建的线程。调用该函数后,线程将开始执行。 3. rt_err_t rt_thread_delete(rt_thread_t thread): 该函数用于删除一个指定的线程。可以在其他线程中调用该函数,也可以在线程自身中调用。 4. rt_err_t rt_thread_suspend(rt_thread_t thread): 该函数用于暂停一个指定的线程,使其进入挂起状态。 5. rt_err_t rt_thread_resume(rt_thread_t thread): 该函数用于恢复一个已暂停的线程,使其从挂起状态恢复执行。 6. void rt_thread_yield(void): 该函数用于主动放弃当前线程的执行权,将CPU时间片让给其他优先级更高的就绪线程。 7. rt_err_t rt_thread_delay(rt_tick_t tick): 该函数用于将当前线程延时指定的时间,单位为时钟节拍。 8. rt_thread_t rt_thread_self(void): 该函数用于获取当前线程的句柄。 这些是Rt_thread中常用的函数,通过它们可以实现线程的创建、启动、挂起、恢复、延时等操作。需要根据具体的应用场景和需求选择合适的函数进行使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值