How to write a time delay function?

C

Chen Shusheng

Hello,
I want to write a time delay function like "Timedelay(float time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?
 
M

markpapadakis

Chen said:
Hello,
I want to write a time delay function like "Timedelay(float time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

usleep() is one way to do it ( whereas it is supported ).
 
R

Richard Heathfield

Chen Shusheng said:
Hello,
I want to write a time delay function like "Timedelay(float
time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

#include <time.h>

void Timedelay(float time_lenth)
{
time_t start = time(NULL);
time_t end;
do
{
end = time(NULL);
} while(difftime(end, start) < time_lenth);
}

(Presumably 'lenth' is meaningful somewhere in the world.)
 
C

Chen Shusheng

Thanks.
Richard Heathfield said:
Chen Shusheng said:


#include <time.h>

void Timedelay(float time_lenth)
{
time_t start = time(NULL);
time_t end;
do
{
end = time(NULL);
} while(difftime(end, start) < time_lenth);
}

(Presumably 'lenth' is meaningful somewhere in the world.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
R

Richard Bos

Chen Shusheng said:
I want to write a time delay function like "Timedelay(float time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

You use a system-specific function, which you can ask for in a
system-specific newsgroup.
Alternatively, you _could_ use a busy-loop, and be killed by your
systems manager for tying up the system.

Richard
 
K

Keith Thompson

Richard Heathfield said:
Chen Shusheng said:

#include <time.h>

void Timedelay(float time_lenth)
{
time_t start = time(NULL);
time_t end;
do
{
end = time(NULL);
} while(difftime(end, start) < time_lenth);
}

(Presumably 'lenth' is meaningful somewhere in the world.)

That's a portable way to do it, but not a good way to do it. By
calling time() repeatedly in a loop, it's likely (on a
multi-processing system) to gobble lots of CPU time.

Most systems provide a way to do this kind of thing, though standard C
doesn't.
 
C

Chen Shusheng

Richard Bos said:
You use a system-specific function, which you can ask for in a
system-specific newsgroup.
Alternatively, you _could_ use a busy-loop, and be killed by your
systems manager for tying up the system.

Richard

My system is windowsXP. Where should I post to get the answer? Could you pls
indicate?
 
R

Richard Heathfield

Keith Thompson said:

That's a portable way to do it, but not a good way to do it.

He didn't ask for a good way to do it. By posting in comp.lang.c, he asked
for a comp.lang.c way to do it.
 
J

John F

Nils O. Selåsdal said:
Start at http://msdn.microsoft.com/ . If you need a newsgroup -
query
your news server for windows programming groups.

The things to look for are SetTimer and KillTimer... These are
Windows-API and thus off topic here. Just to provide a reference for
future lookups. If you need higher resolution use the
multimedia-timer. It's also described there.

HTH
 
K

Keith Thompson

Richard Heathfield said:
Keith Thompson said:



He didn't ask for a good way to do it. By posting in comp.lang.c, he asked
for a comp.lang.c way to do it.

No, he asked for a way to do it. If he were knowledgeable enough to
know in detail what's a comp.lang.c question and what isn't, he
wouldn't have had to ask in the first place.

People ask off-topic questions here all the time. The usual response
is to tell the OP that the question is off-topic and suggest a better
place to ask.

To someone who doesn't know C very well, it's not unreasonable to
think that the language might provide a good time delay function.

I can understand giving deliberately bad answers to "do my homework"
questions, but the OP was just looking for information.

In fact, the OP's question was very close to FAQ 19.37, "How can I
implement a delay, or time a user's response, with sub-second
resolution?".
 
K

Keith Thompson

Chen Shusheng said:
I want to write a time delay function like "Timedelay(float time_lenth){}".
When execute it, it will delay some seconds as long as "time_lenth"
indicating. Could you help on how to write such a function?

The comp.lang.c FAQ is at <http://www.c-faq.com/>. See question
19.37, "How can I implement a delay, or time a user's response, with
sub-second resolution?". (Quick summary: There's no good portable way
to do this, but there are a number of system-specific solutions.)
 
R

Richard Heathfield

Keith Thompson said:
No, he asked for a way to do it.

He got one. :)
If he were knowledgeable enough to
know in detail what's a comp.lang.c question and what isn't, he
wouldn't have had to ask in the first place.

Not necessarily true. Plenty of people know what is topical in comp.lang.c
and ask topical questions.
People ask off-topic questions here all the time.

The question was not off-topic.

In fact, the OP's question was very close to FAQ 19.37, "How can I
implement a delay, or time a user's response, with sub-second
resolution?".

True enough - although, as you clearly spotted, he actually asked for a
resolution in seconds, rather than anything finer.
 
M

Mark McIntyre

My system is windowsXP. Where should I post to get the answer? Could you pls
indicate?

The very first thing you should do is search the MSDN or your compiler
documentation. Typically such functions tell are used to make your
programme "sleep". Thats a hint for the search.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

The things to look for are SetTimer and KillTimer...

Euh, no. but thats why we don't ask or answer system-specific
questions here.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

CBFalconer

#INCLUDE <WINDOWS.H>

...
SLEEP(1000) /*in milliseconds*/

There is no such directive as #INCLUDE. You may be thinking of
#include. If so, there is no such header as <WINDOWS.H> (or
<windows.h>) in standard C. After which there is no such standard
function as SLEEP() in standard C.

Standard C, as described in the various ISO standards, is the
subject of discussion here. Any peculiar non-standard systems are
OFF-TOPIC.
 
J

John F

Mark said:

Why not? :) It can be done. BTDT. It is as portable and standardized
as any way to achieve this...
but thats why we don't ask or answer system-specific
questions here.

*gg* I know. But if you know better, why not correct it? IMHO under
windows SetTimer and KillTimer are excellent for timing in the range
of milliseconds. They are quite precise too. I even use them in (god
help me here in c.l.c.) VB. In C there are other possibilities but I
usually write a handler for WM_TIMER and things are done. Nothing to
worry any more. If you want a delay, use a lock-variable that is set
on timer start and release it in the callback function. Highest
precision you can imagine under windoze... If you ar not satisfied yet
use the multimedia-timer - then it's even possible to do timing in the
range of µs.

All not Standard C, all not portable. It just "works". So I'll hop out
of this obviously off-topic stuff here :)

If you want to shout at me, do so. I have no problem with it.
 
K

Keith Thompson

John F said:
Why not? :) It can be done. BTDT. It is as portable and standardized
as any way to achieve this...


*gg* I know. But if you know better, why not correct it?
[snip]

Because this isn't the place for it.

Assuming SetTimer and KillTimer are specific to MS Windows, there are
a number of Windows-specific newsgroups full of people who know all
about this stuff. comp.lang.c, on the other hand, is constantly
inundated with off-topic posts; carring on off-topic technical
discussions here would only make it worse, and make things more
difficult for those of us who want to talk about C.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top