Linux下的sleep()和sched_yield()
一、sched_yield()的man手冊描述如下:
? 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.
? 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.
? 翻譯一下,sched_yield()會讓出當前線程的CPU占有權,然后把線程放到靜態(tài)優(yōu)先隊列的尾端,然后一個新的線程會占用CPU.那么這個和sleep()有啥區(qū)別呢?
? sched_yield()這個函數(shù)可以使用另一個級別等于或高于當前線程的線程先運行。如果沒有符合條件的線程,那么這個函數(shù)將會立刻返回然后繼續(xù)執(zhí)行當前線程的程序。
? 而sleep則是等待一定時間后等待CPU的調(diào)度,然后去獲得CPU資源。
? Strategic calls to sched_yield() can improve performance by giving other threads or processes a chance to run when (heavily) contented resources(e.g.,mutexes) have been released by the caller.
? 翻譯一下,有策略的調(diào)用sched_yield()能在資源競爭情況很嚴重時,通過給其他的線程或進程運行機會的方式來提升程序的西能。也就是說,調(diào)用sched_yield()能讓你的當前線程讓出資源,通過一定的策略調(diào)用sched_yield()滿足你的業(yè)務要求可以保證各個線程或進程都有機會運行。
參考鏈接:https://blog.csdn.net/zk3326312/article/details/79624199
總結
以上是生活随笔為你收集整理的Linux下的sleep()和sched_yield()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最全的http头部信息分析(转载)
- 下一篇: c库函数-strtol()介绍