sleep(0)与sched_yield()

结论:

       如果你是为了耗掉一个机器周期 ,那直接asm ("nop") ,

  如果是为了让权,建议把 所有使用 sleep(0)  换成 sched_yield() ;

 

sched_yield() 的man手册:

SYNOPSIS

        #include <sched.h>

        int sched_yield(void);

DESCRIPTION
       sched_yield()  causes  the  calling  thread to relinquish the CPU.  The  thread is moved to the end of the queue for its static priority  and  a  new thread gets to run.

(sched_yield()会让出当前线程的CPU占有权,然后把线程放到静态优先队列的尾端,然后一个新的线程会占用CPU。

【如果没有符合条件的线程,那么这个函数将会立刻返回然后继续执行当前线程的程序。】)

RETURN VALUE
       On  success,  sched_yield()  returns  0.  On error, -1 is returned, and errno is set appropriately.

ERRORS

       In the Linux implementation, sched_yield() always succeeds.

什么时候使用:

Strategic calls to sched_yield()  can  improve  performance  by  giving other  threads  or processes  a chance to run when (heavily) contended  resources (e.g., mutexes) have been  released  by  the  caller.

(有策略的调用sched_yield()能在资源竞争情况很严重时,通过给其他的线程或进程运行机会的方式来提升程序的性能)

 sleep(0):

特点:非主动让权,调用了 deactivate_task(),因此延迟高,和sched_yield()慢了一个数量级。

主要目的是:交出当前线程的执行权,让CPU去执行其他线程。也就是放弃当前线程的时间片,转而执行其他线程。

 

感谢:

sleep(0)、usleep(0)与sched_yield() 调度

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值