windows线程yield以及Sleep(0)和SwitchToThread之间的区别

摘要:
在C++自定义线程函数中调用自定义yield()接口。在Windows上,调用SwitchToThread。时间片只能给具有相同或更高优先级的线程;以下是MSDN上Sleep函数的描述:Sleep(0)已成为所有可调度线程的调度程序。

C++的自定义线程函数内调用了一个自定义的yield()接口。

在windows上是调用了SwitchToThread来实现的,linux是pthread_yield实现的。

Sleep(0):时间片只能让给优先级相同或更高的线程; 
SwitchToThread():只要有可调度线程,即便优先级较低,也会让其调度。

下面是MSDN上对Sleep函数的描述:

The time interval for which execution is to be suspended, in milliseconds.

A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.

Windows XP/2000:  A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. This behavior changed starting with Windows Server 2003.

可以看到,从2003 server开始,Sleep(0)变成了调度所有可调度线程,跟SwitchToThread差不多了。

免责声明:文章转载自《windows线程yield以及Sleep(0)和SwitchToThread之间的区别》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇idea绘制activity流程图中文乱码解决windows10 Anaconda3安装教程下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JavaNetty拆包粘包(二)

netty 使用 tcp/ip 协议传输数据。而 tcp/ip 协议是类似水流一样的数据传输方式。多次 访问的时候有可能出现数据粘包的问题,解决这种问题的方式如下: 定长数据流  客户端和服务器,提前协调好,每个消息长度固定。(如:长度 10)。如果客户端或服 务器写出的数据不足 10,则使用空白字符补足(如:使用空格)。  /** * 1. 单线程组...

[译]GPUView

【本文翻译自GPUView的开发者Matt的blog.  https://graphics.stanford.edu/~mdfisher/GPUView.html  】 【 GPUview可以在 https://docs.microsoft.com/en-us/windows-hardware/get-started/adk-install 这里下载到】...

Android学习笔记十:异步处理

转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7520700.html 一:基础概念 UI线程:当Android程序第一次启动时,Android会同时启动一条主线程(Main Thread),主线程主要负责处理与UI相关的事件,如用户的按键事件、屏幕绘图事件,并把相关的事件分发到对应的组件进行处理。主线程通常又被称为...

Qt跨线程调用错误解析及解决办法

错误提示:Error: Cannot create children for a parent that is in a different thread. 错误案例分析 新建SerialLink子线程,继承QThread,并重写它的run(),调用 start()函数时自动调用重载的run()函数。在主线程中创建SerialLink类的对象。 串口_po...

C#中yield return用法分析

 staticList<int> GetInitialData() {   returnnewList<int>(){1,2,3,4}; } 打印出所有值大于2的元素   不使用yield return的实现 staticIEnumerable<int> FilterWithoutYield() {   List&l...

mysql8中窗口函数

引用自: https://blog.csdn.net/yeshang_lady/article/details/102728513 在以前的MySQL版本中是没有窗口函数的,直到MySQL8.0才引入了窗口函数。窗口函数是对查询中的每一条记录执行一个计算,并且这个计算结果是用与该条记录相关的多条记录得到的。 1.窗口函数与聚合函数 窗口函数与聚合函数很像...