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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Ubuntu 16.4安装Gitlab runner
下一篇:EA逆向源码生成模型提示“遇到未知的符号”

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月21日 21时46分20秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章