Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
4.40/5 (3 votes)

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

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);

Does the above approach have any problems?

Posted
Updated 16-Oct-09 4:10am
v6
Comments
Albert Holguin 19-Dec-11 13:09pm    
Are all those tags really applicable (I doubt it)? Tag appropriately please.

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


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!
 
Share this answer
 
v2
Yeah, that's not happening...

Why 1 microsecond, FFS? What are you trying to do? Because there may well be a better way.
 
Share this answer
 
v2
Comments
Albert Holguin 19-Dec-11 13:14pm    
That's a good question...
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.
 
Share this answer
 
Comments
fjdiewornncalwe 19-Dec-11 13:23pm    
Nice answer, but the question is over 2 years old... :)
Chuck O'Toole 19-Dec-11 13:25pm    
Ha, didn't see that. It popped up on the radar because of solution 3. Prashant answered the original question a few 20 minutes before I did so I just followed through.
Albert Holguin 19-Dec-11 14:53pm    
I didn't notice that either... it was in the "active questions" list... oh well...
Hi System.Threading.Thread.Sleep(your time).
 
Share this answer
 
Comments
Mohibur Rashid 19-Dec-11 20:43pm    
you provide a .Net solution, and as you can see from above discussion its clear that in a multi-tasking, non real-time operating system sleep wont do a big deal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900