How to sleep in miliseconds, usleep() problem

D

D. Susman

Hi,

Is there a platform-independent way to sleep in miliseconds? I have
used usleep but some strange behaviour has occured. I expect that
usleep(1000) and sleep(1) shall trigger 1 second interval delays. sleep
(1) does so, but usleep(1000) fails to accomplish that.

Any advice is appreciated, thanks
 
J

jason.cipriani

Hi,

Is there a platform-independent way to sleep in miliseconds? I have
used usleep but some strange behaviour has occured. I expect that
usleep(1000) and sleep(1) shall trigger 1 second interval delays. sleep
(1) does so, but usleep(1000) fails to accomplish that.

Any advice is appreciated, thanks

sleep() is not platform-independent.

usleep() is not platform-independent either. Also, it takes it's
argument in microseconds, not milliseconds. usleep(1000) sleeps for
1000 microseconds, that's 1 millisecond, not 1 second. You can't use
usleep() to sleep for 1 second -- usleep(1000000) is not valid. See
http://linux.die.net/man/3/usleep .

Neither sleep() nor usleep() are defined by C++.

There is no platform-independent way to sleep in milliseconds (or in
any unit). You might find something in boost (e.g. boost::thread::sleep
()) or some other library.

Jason
 
K

Kai-Uwe Bux

D. Susman said:
Hi,

Is there a platform-independent way to sleep in miliseconds?
No.

I have
used usleep but some strange behaviour has occured. I expect that
usleep(1000) and sleep(1) shall trigger 1 second interval delays. sleep
(1) does so, but usleep(1000) fails to accomplish that.

Try usleep(1000000); usleep() uses microseconds, not milliseconds.


Best

Kai-Uwe Bux
 
M

Michael DOUBEZ

D. Susman a écrit :
Hi,

Is there a platform-independent way to sleep in miliseconds?

No. And c++ doesn't define any stabdard way of achieving that. AFAIK
neither does C.
I have
used usleep but some strange behaviour has occured. I expect that
usleep(1000) and sleep(1) shall trigger 1 second interval delays. sleep
(1) does so, but usleep(1000) fails to accomplish that.

usleep take microseconds in parameters, not milliseconds.
try:
usleep(1000000)
Any advice is appreciated, thanks

If you really have protability issue, try using select. It will work on
POSIX systems.
Google for a way to do it; there should be some code available.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top