Topic: Is there a standard way to add a delay in C++ (similar to sleep)
Author: rgetov@hotmail.com (Radoslav Getov)
Date: Mon, 28 Jan 2002 18:33:32 GMT Raw View
Brent Nye <brentnye@excite.com> wrote in message news:<3C52F200.2755EBF7@excite.com>...
> I was thinking of just a for loop or something, but then I am just guessing at
> the timing and it would depend on the performance of the machine.
>
> How about a fixed amount of time for a delay, is there a simple way to do this?
>
> Thanks,
>
> Brent
>
Sure, something like:
time_t begin = time(NULL);
while (time(NULL) < begin + delay_in_secs)
;
It's rather coarse and CPU-gready, though. For more accurate
timing, you should use environment-specific stuff. If your environment
appears to be Win32, use Sleep (delay_time_in_ms). I believe there is
something similar in posix (a multi-threading standard for C).
Which, by the way, raises a question. Probably standartizing threads is not
an easy thing to do, so, as a result, why we don't have threads notion at all
in C++. OTOH it looks like small things like high resolution time
measurement are not that difficult. So why don't we have at least these?
Radoslav Getov
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Marek Przeczek <marek.przeczek@st.ms.mff.cuni.cz>
Date: Mon, 28 Jan 2002 19:47:25 GMT Raw View
Brent Nye wrote:
> I was thinking of just a for loop or something, but then I am just
> guessing at the timing and it would depend on the performance of the
> machine.
>
> How about a fixed amount of time for a delay, is there a simple way to do
> this?
>
> Thanks,
>
> Brent
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
if you think to use it on solaris, don't use usleep() function call while
using signals in your program. it is a bit creepy and uses a SIGALRM
for delay measurement. better use sleep(), poll() or select() or
system dependant function call...
marek
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Richard Smith" <richard@ex-parrot.com>
Date: Tue, 29 Jan 2002 14:33:21 CST Raw View
"Marek Przeczek" <marek.przeczek@st.ms.mff.cuni.cz> wrote in message
news:a31qbn$15or$1@cuce.ruk.cuni.cz...
> James Kuyper Jr. wrote:
>
> > Brent Nye wrote:
> >>
> >> I was thinking of just a for loop or something, but then I am just
> >> guessing at the timing and it would depend on the performance of the
> >> machine.
> >>
> >> How about a fixed amount of time for a delay, is there a simple way to
do
> >> this?
> >
> > There is a standard way to do it, but the relevant standard isn't the
> > C++ standard. There's a function named sleep() provided for by some Unix
> > standards (there's several to choose from, unfortunately), which takes
> > an unsigned argument indicating the number of seconds to sleep. If your
> > system isn't covered by one of those standards, you might have to try
> > something else.
> >
> > ---
> > [ comp.std.c++ is moderated. To submit articles, try just posting
with ]
> > [ your news-reader. If that fails, use
lto:std-c++@ncar.ucar.edu ]
> > [ --- Please see the FAQ before
]
> > [ FAQ:
arch.att.com/~austern/csc/faq.html ]
>
> that's true, you can use sleep() if you need a delay in seconds, but
> if you want to wait eg. for 200 milliseconds, you cannot use it:
The Single Unix Specification, Version 2, defines the usleep function which
takes a number in microseconds ( < 1 000 000 ). If your platform supports
this, that might help.
--
Richard Smith
======================================= MODERATOR'S COMMENT:
Please don't overquote.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Marek Przeczek <marek.przeczek@st.ms.mff.cuni.cz>
Date: Tue, 29 Jan 2002 15:09:50 CST Raw View
Richard Smith wrote:
>
> "Marek Przeczek" <marek.przeczek@st.ms.mff.cuni.cz> wrote in message
> news:a31qbn$15or$1@cuce.ruk.cuni.cz...
>> James Kuyper Jr. wrote:
>>
>> > Brent Nye wrote:
>> >>
>> >> I was thinking of just a for loop or something, but then I am just
>> >> guessing at the timing and it would depend on the performance of the
>> >> machine.
>> >>
>> >> How about a fixed amount of time for a delay, is there a simple way to
> do
>> >> this?
>> >
>> > There is a standard way to do it, but the relevant standard isn't the
>> > C++ standard. There's a function named sleep() provided for by some
>> > Unix standards (there's several to choose from, unfortunately), which
>> > takes an unsigned argument indicating the number of seconds to sleep.
>> > If your system isn't covered by one of those standards, you might have
>> > to try something else.
>> >
>> > ---
>> > [ comp.std.c++ is moderated. To submit articles, try just posting
> with ]
>> > [ your news-reader. If that fails, use
> lto:std-c++@ncar.ucar.edu ]
>> > [ --- Please see the FAQ before
> ]
>> > [ FAQ:
> arch.att.com/~austern/csc/faq.html ]
>>
>> that's true, you can use sleep() if you need a delay in seconds, but
>> if you want to wait eg. for 200 milliseconds, you cannot use it:
>
> The Single Unix Specification, Version 2, defines the usleep function
> which
> takes a number in microseconds ( < 1 000 000 ). If your platform supports
> this, that might help.
>
> --
> Richard Smith
>
>
>
>
> ======================================= MODERATOR'S COMMENT:
> Please don't overquote.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
read my notice about usleep() above in the thread
marek
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Brent Nye <brentnye@excite.com>
Date: Sun, 27 Jan 2002 01:34:33 GMT Raw View
I was thinking of just a for loop or something, but then I am just guessing at
the timing and it would depend on the performance of the machine.
How about a fixed amount of time for a delay, is there a simple way to do this?
Thanks,
Brent
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sun, 27 Jan 2002 16:04:05 GMT Raw View
Brent Nye wrote:
>
> I was thinking of just a for loop or something, but then I am just guessing at
> the timing and it would depend on the performance of the machine.
>
> How about a fixed amount of time for a delay, is there a simple way to do this?
There is a standard way to do it, but the relevant standard isn't the
C++ standard. There's a function named sleep() provided for by some Unix
standards (there's several to choose from, unfortunately), which takes
an unsigned argument indicating the number of seconds to sleep. If your
system isn't covered by one of those standards, you might have to try
something else.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Marek Przeczek <marek.przeczek@st.ms.mff.cuni.cz>
Date: Sun, 27 Jan 2002 21:44:27 GMT Raw View
James Kuyper Jr. wrote:
> Brent Nye wrote:
>>
>> I was thinking of just a for loop or something, but then I am just
>> guessing at the timing and it would depend on the performance of the
>> machine.
>>
>> How about a fixed amount of time for a delay, is there a simple way to do
>> this?
>
> There is a standard way to do it, but the relevant standard isn't the
> C++ standard. There's a function named sleep() provided for by some Unix
> standards (there's several to choose from, unfortunately), which takes
> an unsigned argument indicating the number of seconds to sleep. If your
> system isn't covered by one of those standards, you might have to try
> something else.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
that's true, you can use sleep() if you need a delay in seconds, but
if you want to wait eg. for 200 milliseconds, you cannot use it:
use select() to solve this problem, this function is typically implemented
on most systems (including win32 and unix systems) because it is a part
of stardard TCP communication interface...
use it in this way:
select( 0, NULL, NULL, NULL, &tv)
where tv is a variable of 'timeval' type
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Ramesh Kadambi" <ramesh.kadambi@verizon.net>
Date: Mon, 28 Jan 2002 06:52:59 GMT Raw View
On Windows NT select is not a good method to use as a timer. There are two
ways you could use a timer.
Use SetTimer for windows based applications, or Sleep ( uses milliseconds ).
If you want a more accurate timer you could use a multimedia timer (be aware
that this executes in a seperate thread and you may need to use
synchronization).
Ramesh
"Marek Przeczek" <marek.przeczek@st.ms.mff.cuni.cz> wrote in message
news:a31qbn$15or$1@cuce.ruk.cuni.cz...
> James Kuyper Jr. wrote:
>
> > Brent Nye wrote:
> >>
> >> I was thinking of just a for loop or something, but then I am just
> >> guessing at the timing and it would depend on the performance of the
> >> machine.
> >>
> >> How about a fixed amount of time for a delay, is there a simple way to
do
> >> this?
> >
> > There is a standard way to do it, but the relevant standard isn't the
> > C++ standard. There's a function named sleep() provided for by some Unix
> > standards (there's several to choose from, unfortunately), which takes
> > an unsigned argument indicating the number of seconds to sleep. If your
> > system isn't covered by one of those standards, you might have to try
> > something else.
> >
> > ---
> > [ comp.std.c++ is moderated. To submit articles, try just posting
with ]
> > [ your news-reader. If that fails, use
lto:std-c++@ncar.ucar.edu ]
> > [ --- Please see the FAQ before
]
> > [ FAQ:
arch.att.com/~austern/csc/faq.html ]
>
> that's true, you can use sleep() if you need a delay in seconds, but
> if you want to wait eg. for 200 milliseconds, you cannot use it:
>
> use select() to solve this problem, this function is typically implemented
> on most systems (including win32 and unix systems) because it is a part
> of stardard TCP communication interface...
>
> use it in this way:
>
> select( 0, NULL, NULL, NULL, &tv)
>
> where tv is a variable of 'timeval' type
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]