Linux下的sleep()和sched_yield()
一、sched_yield()的man手冊(cè)描述如下:
? 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()會(huì)讓出當(dāng)前線程的CPU占有權(quán),然后把線程放到靜態(tài)優(yōu)先隊(duì)列的尾端,然后一個(gè)新的線程會(huì)占用CPU.那么這個(gè)和sleep()有啥區(qū)別呢?
? sched_yield()這個(gè)函數(shù)可以使用另一個(gè)級(jí)別等于或高于當(dāng)前線程的線程先運(yùn)行。如果沒(méi)有符合條件的線程,那么這個(gè)函數(shù)將會(huì)立刻返回然后繼續(xù)執(zhí)行當(dāng)前線程的程序。
? 而sleep則是等待一定時(shí)間后等待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()能在資源競(jìng)爭(zhēng)情況很?chē)?yán)重時(shí),通過(guò)給其他的線程或進(jìn)程運(yùn)行機(jī)會(huì)的方式來(lái)提升程序的西能。也就是說(shuō),調(diào)用sched_yield()能讓你的當(dāng)前線程讓出資源,通過(guò)一定的策略調(diào)用sched_yield()滿(mǎn)足你的業(yè)務(wù)要求可以保證各個(gè)線程或進(jìn)程都有機(jī)會(huì)運(yùn)行。
參考鏈接:https://blog.csdn.net/zk3326312/article/details/79624199
總結(jié)
以上是生活随笔為你收集整理的Linux下的sleep()和sched_yield()的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 最全的http头部信息分析(转载)
- 下一篇: 《深入理解LINUX网络技术内幕》小记1