How to simulate a clock?

C

cpptutor2000

Could some C guru suggest some possible solution to my problem? I am
trying to simulate a clock and I have tried using 'gettimeofday' and
some related C library functions, but I am not getting what I want -
the clock should be ticking away on its own, and from time to time, an
observer will
read the clock value, and decide to do something else. Any hints,
suggestions would be greatly appreciated. Thanks in advance for your
help.
 
E

Eric Sosman

Could some C guru suggest some possible solution to my problem? I am
trying to simulate a clock and I have tried using 'gettimeofday' and
some related C library functions, but I am not getting what I want -
the clock should be ticking away on its own, and from time to time, an
observer will
read the clock value, and decide to do something else. Any hints,
suggestions would be greatly appreciated. Thanks in advance for your
help.

Standard C provides two ways to measure the passage of time.
The time() function returns the current "wall clock time," the
time you would observe by reading your watch and calendar. The
clock() function returns the amount of CPU time your program
has used since some fixed but arbitrary moment; subtracting two
clock() values gives you the amount of CPU time consumed between
the two calls. Both the precision and accuracy of these functions
vary from implementation to implementation; for time(), even the
encoding is implementation-dependent (but additional functions
exist to help make sense of the values).

There is no gettimeofday() function in Standard C. You are
probably thinking of a function provided by POSIX and other
Unix-ish Standards; if you have questions about POSIX-specific
facilities, try asking them in comp.unix programmer.

Both time() and clock() "tick away on their own," unless
you're using the phrase in a way not familiar to me. Your
program can call either or both whenever it pleases, and can
decide (or not) to change its behavior depending on the readings.
 
M

Malcolm

Could some C guru suggest some possible solution to my problem? I am
trying to simulate a clock and I have tried using 'gettimeofday' and
some related C library functions, but I am not getting what I want -
the clock should be ticking away on its own, and from time to time, an
observer will
read the clock value, and decide to do something else. Any hints,
suggestions would be greatly appreciated. Thanks in advance for your
help.
Most platforms give you some way of accessing "timer interrupts", or, in the
case of Windowing systems, sent you "timer messages". If you're lucky you
might even get a vertical blank interrupt when the screen refreshes.
ANSI C doesn't provide support for multi-threading, and windowing libraries
are platform specific. So we can't answer your question here.
 

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,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top