How to sleep during millisecond

Y

Yeounkun, Oh

Hello.

Sleep (x) function make a process sleep during x seconds.

but, how to sleep during milliseconds...

Pls. help me.

Thank you. Regards.
 
P

Paul L Daniels

Hello.

Sleep (x) function make a process sleep during x seconds.

but, how to sleep during milliseconds...

try 'usleep()'

Not sure if that'll help.
 
K

Keith Thompson

Yeounkun said:
Sleep (x) function make a process sleep during x seconds.

but, how to sleep during milliseconds...

There is no sleep() (or Sleep()) function in standard C, though many
systems provide such a function. Many systems probably also provide a
way to sleep for a specified number of milliseconds. You'll need to
ask in a newsgroup that's specific to your system (Windows, Unix,
Linux, whatever).
 
T

Thomas Matthews

Paul said:
try 'usleep()'

Not sure if that'll help.

Which compiler is usleep() found in?
I didn't find it in my copy of the C language specification,
could you let us know where it is?

Since where off-topic, perhaps you could use:
wait()
suspend()
delay()
ms_delay()
us_delay()

You could also use a timer and poll the "done" bit to
see when it is finished. Or you could use an interrupt
system.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Jens.Toerring

Which compiler is usleep() found in?
I didn't find it in my copy of the C language specification,
could you let us know where it is?
Since where off-topic, perhaps you could use:
wait()
suspend()
delay()
ms_delay()
us_delay()

Don't forget about nanosleep(), which is preferable to usleep()
on UNIX systems...
Regards, Jens
 
M

Merrill & Michele

"Yeounkun, Oh"
Sleep (x) function make a process sleep during x seconds.

but, how to sleep during milliseconds...

Pls. help me.

Thank you. Regards.

Jerryrig difftime() found in time.h . If you have a chip made in the last
fifteen years, I think you can do it in standard C with code that would be
nonsensical on a slow machine. I think that is far different than
non-complying or undefined behavior. Google for Chris Torek. MPJ
 
J

Jens.Toerring

Merrill & Michele said:
"Yeounkun, Oh"
Jerryrig difftime() found in time.h . If you have a chip made in the last
fifteen years, I think you can do it in standard C with code that would be
nonsensical on a slow machine. I think that is far different than
non-complying or undefined behavior. Google for Chris Torek. MPJ

But there are no guarantees made about the time resolution of difftime()
(on POSIX systems it's not better than second resolution) and the OP
wants milliseconds .(And beside that what he wants seems to be to sleep
and not busy-loop.)
Regards, Jens
 
M

Merrill & Michele

Jens:
But there are no guarantees made about the time resolution of difftime()
(on POSIX systems it's not better than second resolution) and the OP
wants milliseconds .(And beside that what he wants seems to be to sleep
and not busy-loop.)

To be honest with you, I can't think of a situation makes sense for sleeping
for a matter of milliseconds. Difftime is guaranteed to return a long in
seconds. How does a computer distinguish sleep from a busy loop? MPJ
 
J

Jens.Toerring

To be honest with you, I can't think of a situation makes sense for
sleeping for a matter of milliseconds.

When you're dealing with hardware that requires some timeouts in the
millisecond range it's quite convenient if you don't have to sleep
each time for at least a second;-) And there are also lots of other
cases where a sub-second resolution is important.
Difftime is guaranteed to return a long in seconds.

It returns a double. But that still doesn't guarantee even second
resolution, the standard does not make any statements about the
time resolution of a time_t, it just states that it's an "arith-
metic types capable of representing times".
How does a computer distinguish sleep from a busy loop? MPJ

It's heating the CPU less;-) If you're on a multi-tasking system the
system can schedule other tasks while you're sleeping. Imagine a
situation where you want to e.g. emit a short beep from the speaker
every 5th of a second. With busy-looping that task alone would use
100% CPU time while with a real sleep function it might reduce to a
few percent - so other jobs could be done in between by the machine.

Regards, Jens
 
K

Keith Thompson

Merrill & Michele said:
To be honest with you, I can't think of a situation makes sense for sleeping
for a matter of milliseconds. Difftime is guaranteed to return a long in
seconds. How does a computer distinguish sleep from a busy loop? MPJ

difftime() returns a double, not a long.

I don't know what you mean by "How does a computer distinguish", but
there's a major difference in a multiprocessing system. A busy loop
causes the current program to consume CPU time; a sleep typically
allows other processes to run. The C standard doesn't deal with
multiprocessing, but a program (even a strictly conforming one) that
executes a busy loop is likely to cause problems.
 
L

Lawrence Kirby

On Fri, 10 Dec 2004 20:18:53 +0000, Jens.Toerring wrote:

....
It returns a double. But that still doesn't guarantee even second
resolution, the standard does not make any statements about the
time resolution of a time_t, it just states that it's an "arith-
metic types capable of representing times".

Which is significant because difftime() takes time_t arguments and
therefore its resolution is limited to that of time_t on that
implementation. A double return type provides a simple and useful
interface that can handle finer resolution when it is available.

Lawrence
 
R

Richard Bos

It's heating the CPU less;-) If you're on a multi-tasking system the
system can schedule other tasks while you're sleeping. Imagine a
situation where you want to e.g. emit a short beep from the speaker
every 5th of a second. With busy-looping that task alone would use
100% CPU time while with a real sleep function it might reduce to a
few percent - so other jobs could be done in between by the machine.

And let me put on my sysadmin hat once again and add to that that if you
think you'll get away with using a busy-loop on any of my systems,
you're sadly mistaken. Hogging resources is a very good reason to ditch
programs - and programmers.

Richard
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top