The difference between sleep(), wait(), and yield() in human terms.

It all eventually makes its way down to the OS’s scheduler, which hands out timeslices to processes and threads.

sleep(n) says “I’m done with my timeslice, and please don’t give me another one for at least n milliseconds.” The OS doesn’t even try to schedule the sleeping thread until requested time has passed.

yield() says “I’m done with my timeslice, but I still have work to do.” The OS is free to immediately give the thread another timeslice, or to give some other thread or process the CPU the yielding thread just gave up.

.wait() says “I’m done with my timeslice. Don’t give me another timeslice until someone calls notify().” As with sleep(), the OS won’t even try to schedule your task unless someone calls notify() (or one of a few other wakeup scenarios occurs).

Threads also lose the remainder of their timeslice when they perform blocking IO and under a few other circumstances. If a thread works through the entire timeslice, the OS forcibly takes control roughly as if yield() had been called, so that other processes can run.

You rarely need yield(), but if you have a compute-heavy app with logical task boundaries, inserting a yield() might improve system responsiveness (at the expense of time — context switches, even just to the OS and back, aren’t free). Measure and test against goals you care about, as always.

时间: 2024-11-05 03:49:17

The difference between sleep(), wait(), and yield() in human terms.的相关文章

PHP生成器Generators

下文的第一个逐行读取文件例子用三种方式实现;普通方法,迭代器和生成器,比较了他们的优缺点,很好,可以引用到自己的代码中 ,支持的php版本(PHP 5 >= 5.5.0) 后面的yield讲解,得逐行翻译理解 Request for Comments: Generators Date: 2012-06-05 Author: Nikita Popov [email protected] Status: Implemented Introduction Generators provide an e

It's import to know before start learning DW/BI

Data Warehousing and Business Intelligence Differences Between Data Warehousing and Business Intelligence Filed under: Business Intelligence,Data Warehousing — Vincent Rainardi @ 6:38 pm Tags: Business Intelligence, Data Warehousing Try asking your c

Discrete.Differential.Geometry-An.Applied.Introduction(sig2013) 笔记

DISCRETE DIFFERENTIAL GEOMETRY : AN APPLIED INTRODUCTION Last updated: November 19, 2015 ======================================== Ch 1. INTRODUCTION 把exterior calculus作为模型处理的语言.the exterior calculus of differential forms is the modern language of dif

人工智能有简单的算法吗?Appendix: Is there a simple algorithm for intelligence?

In this book, we've focused on the nuts and bolts of neural networks: how they work, and how they can be used to solve pattern recognition problems. This is material with many immediate practical applications. But, of course, one reason for interest

【Python笔记】如何理解python中的generator functions和yield表达式

本篇笔记记录自己对Python的generator functions和yield表达式的理解. 1. Generator Functions Python支持的generator functions语法允许我们定义一个行为与iterator类似的函数,它可以被用在需要循环调用的场合.与普通函数相比,generator functions只是在函数定义中多了1个yield表达式,除此之外,没有其它特别之处. 当generator函数被创建时,python解释器会自动为它实现iteration p

python: the difference between append and extend

Data Analysis: indoor localization using received signal strength (RSS) An error about list operation in python: append and extend elements We define a list A to storage objects, which the length is unknown, and list B and C to storage the final resu

关于Python中的yield

关于Python中的yield http://www.cnblogs.com/tqsummer/archive/2010/12/27/1917927.html http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何类型,包括列表.元祖等等,实际上,for循环可用于任何“可迭代对象”,这其实就是迭代器 迭代器是一个实现了迭代器协议

ruby yield 关键字用法实例

yield关键字我是这样理解,用它来占一个位置,先标记下这个地方将来要写代码的,等到调用的时候,再来编写具体的代码.有点像函数指针,或者C#里的委托,但其实并不太一样. 写测试接口的时候,每次的assert返回值不一样,但函数体大部分是相同的,只有参数不同.正好最近看到了yield,就熟悉一下用法,可以把assert这部分code写在yield 的位置. 例子主要就是test_nodes这个函数的定义 和 它的调用. Code: 1 def generate_nodes(n=3) 2 retur

lua协程一则报错解决“attempt to yield across metamethod/C-call boundary”

问题 attempt to yield across metamethod/C-call boundary 需求跟如下帖子中描述一致: http://bbs.chinaunix.net/forum.php?mod=viewthread&action=printable&tid=4065715 模拟一个场景,在C中创建出coroutine来执行Lua脚本,并且提供C API给Lua使用,当某些操作可能会阻塞时(如网络I/O),C函数中执行yield将协程切换出去,然后未来的某个时刻,如果条件