Sleep(0)与Sleep(1)区别
发布日期:2021-06-28 22:05:00
浏览次数:3
分类:技术文章
本文共 831 字,大约阅读时间需要 2 分钟。
1、问题
在线程中我们会经常按如下写代码,Sleep(0)比Sleep(1)更容易获得时间片
DWORD WINAPI SampleThread(PVOID pvParam){ while ( true ) { // do something Sleep(1); } return 0x50;}
2、原因
MSDN说明如下:
Remarks This function causes a thread to relinquish the remainder of its time slice and become unrunnable for at least the specified number of milliseconds, after which the thread is ready to run. In particular, if you specify zero milliseconds, the thread will relinquish the remainder of its time slice but remain ready. Note that a ready thread is not guaranteed to run immediately. Consequently, the thread may not run until some time after the specified interval elapses. For more information, see Scheduling Priorities.
Sleep(1)导致线程释放剩余的时间片,并停止运行至少1毫秒,然后恢复为就绪状态。
Sleep(0)导致线程释放剩余的时间片,并立即进入就绪状态。
所以如果有两个同等优先级的线程一个使用Sleep(1),一个使用Sleep(0),后者将更多的抢到时间片。
转载地址:https://blog.csdn.net/yfh1985sdq/article/details/113756525 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月21日 21时46分20秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
log4j2异步发送error日志邮件配置
2019-04-29
redis setnx解决定时任务多节点部署并发问题(分布式锁)
2019-04-29
spring boot使用redis解决session双机问题
2019-04-29
Java Web会话机制,Cookie和Session详解
2019-04-29
基于timestamp和nonce的防止重放攻击方案
2019-04-29
Linux常用基础命令198个
2019-04-29
Linux运维工程师初级面试题
2019-04-29
GA入门
2019-04-29
kettle问题合集
2019-04-29
spark学习笔记
2019-04-29
Tableau学习笔记
2019-04-29
Kettle学习笔记
2019-04-29
airflow问题合集
2019-04-29
sql
2019-04-29
BI分析
2019-04-29
JAVA线程之间如何通信
2019-04-29
java时间工具类
2019-04-29
rabbitmq简单队列使用例子
2019-04-29
rabbitmq工作队列使用例子
2019-04-29
rabbitmq订阅模型-广播使用例子
2019-04-29