Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C++/CLI C MFC ATL WTL STL

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 13-Aug-09 20:48pm
Edited 16-Oct-09 4:10am
0x3c033.1K
v6
Comments
Albert Holguin at 19-Dec-11 13:09pm
   
Are all those tags really applicable (I doubt it)? Tag appropriately please.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Yeah, that's not happening...

Why 1 microsecond, FFS? What are you trying to do? Because there may well be a better way.
  Permalink  
v2
Comments
Albert Holguin at 19-Dec-11 13:14pm
   
That's a good question...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi
System.Threading.Thread.Sleep(your time).
  Permalink  
Comments
johny10151981 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Comments
Marcus Kramer at 19-Dec-11 13:23pm
   
Nice answer, but the question is over 2 years old... :)
Chuck O'Toole at 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 at 19-Dec-11 14:53pm
   
I didn't notice that either... it was in the "active questions" list... oh well...

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

  Print Answers RSS
0 Maciej Los 599
1 OriginalGriff 419
2 CPallini 205
3 RyanDev 175
4 Sergey Alexandrovich Kryukov 170
0 OriginalGriff 5,649
1 Sergey Alexandrovich Kryukov 4,783
2 Peter Leow 2,954
3 Maciej Los 2,689
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 19 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid