我可以使用select代替Sleep吗? [英] Can I use select instead Sleep?

查看:319
本文介绍了我可以使用select代替Sleep吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的程序休眠1微秒,但是Sleep()无法做到这一点.

I need my program to sleep 1 microsecond, but Sleep() can not do this.

那么,我可以使用select()代替Sleep()来做到这一点吗?

So, can I use select() instead of Sleep() to do this?

timeout.tv_sec=0;
timeout.tv_usec=1;
FD_ZERO(&readfd);

ret=select(0,&readfd,NULL,NULL,&timeout);

上述方法是否有问题?

推荐答案

hanlei0000000009写道:
hanlei0000000009 wrote:

我需要让程序睡眠1微秒,但Sleep()无法做到这一点.

I need my program sleep 1 microsecond, but Sleep() can not do this.



其他任何API都不能做到这一点(在Windows下). Windows从未被设计为提供这种功能(它不是实时操作系统).

如果使用类似Sleep(2)的程序,则程序可能会休眠2毫秒,可能是3或4毫秒,甚至100毫秒.对此不做任何保证.线程调度程序会尽最大努力将线程置于不可调度"状态,以尽可能接近您的要求.但是,它几乎永远不可能是精确的.

因此,尽管Windows甚至不能向您保证毫秒级的精度,但您可以忘记微秒级,这是毫秒级精度的1000倍!



Neither can any other API do this (under Windows). Windows was never designed to provide this kind of functionality (it is not a real-time OS).

If you use something like Sleep(2), your program may sleep for 2 milliseconds, may be 3, or 4, or say even 100 ms. There''s no guarantee about this. The thread scheduler tries its levels best to put your thread to an ''unschedulable'' state for as close as possible, to what you''ve asked. But, it can almost never be exact.

So, while Windows cannot even promise you the precision of milliseconds, you can forget microseconds, which is 1000 times the precision of milliseconds!


是的,这没有发生...

为什么1微秒FFS?你想做什么?因为可能会有更好的方法.
Yeah, that''s not happening...

Why 1 microsecond, FFS? What are you trying to do? Because there may well be a better way.


仅因为select()的API定义采用其超时值(以微秒为单位),但这并不意味着每个支持Socket API的操作系统都必须提供微秒的结果在他们的计时器上.实际上,几乎没有操作系统.

正如Rajesh所说,Windows(Unix,Linux,VMS,yada yada)表示您的调用(Sleep(),select()等)将返回的时间不少于"x"时间(毫秒,微秒),但确实如此不保证"不超过" x" "

因此,如果您的应用程序需要此级别的时间限制,则您使用的是错误的操作系统.您需要一个旨在确保高速中断定时的实时内核.
Just because the API definition of select() takes its timeout value in microseconds, that doesn''t mean that every operating system that supports the Socket API has to provide microsecond resultion on their timers. In fact, almost no operating system does.

As Rajesh said, Windows (Unix, Linux, VMS, yada yada) say that your call (Sleep(), select(), etc) will return is no less than ''x'' time (milliseconds, microseconds) but that does not guarantee "no more than ''x''"

So if your application requires this level of time constraint, you''re using the wrong operating system. You need a real-time kernel designed to guarantee high speed interrupt timings.


这篇关于我可以使用select代替Sleep吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆