create a timer Posted on March 14th
Sometimes, you need a timer function. For example, you wanna check some live status every 5 seconds. By using sleep() is just not the way of doing, because the programs suppose to process for other things within this 5 seconds. We need a non-blocking timer function for that.
I created two functions uses timeval structure, and gettimeofday(), does the job of timer. Bellow is the code that illustrate how it works:
SetTimer() are use to set timer for the first time, and CheckTimer() use to check for the previous timer value. If it is times up, it will returns 1 at the same time initiate a new time value based on what you pass in at second argument. By doing that, is to allow flexibility of changing time interval value.
The minimum value for this timer is one second, for timer that works for less than one seconds, replace the SetTimer and CheckTimer as bellow:
P.S. The code must be compile in c++, I uses reference which it is not available in c. You can compile the code with g++ ,
g++ -o mytimer{,.cc}
More examples of gcc/g++ compiler shows here.
Feel free to enhance it, I appreciate any comments regarding modifications, suggestions on how to improve it.
Trackback URL
I think this is blocking call,
Commented Mahesh on March 22nd, 2007.create_timer will send the signal on time elapse and on receiving signal user can take the action and meantime process other things
This is what you are trying to achieve?
something like that, but it do not deal with signal, it just let it passes if it is not his time to execute. The CheckTimer only works in the infinite loop, I use it to deal with animations or periodically live updates or in certain infinite loop thread.
Commented mysurface on March 23rd, 2007.Dude, use select system call to do that. This is a busy tight loop.
Commented durian on March 26th, 2007.Durian:
Commented mysurface on March 26th, 2007.Cool, can illustrate with some sample examples code?
Sorry, didn’t read your post carefully. My bad.
Commented durian on March 27th, 2007.your progrem is good!
I make a progrem like this, but modify a little, look ……
/* main.c */
/* C routine: sample gettimeofday with time in milliseconds
mmc mchirico@users.sourceforge.net
Downloads:
http://prdownloads.sourceforge.net/cpearls/date_calc.tar.gz?download
Quote: http://cc.byexamples.com/20070314/create-a-timer/
*/
#include
#include
#include
#include
#include
const long TIMER_5_SECONDS = 1;
int SetTimer(struct timeval &tv, time_t sec)
{
gettimeofday(&tv, NULL);
tv.tv_sec += sec;
return 1;
}
int CheckTimer(struct timeval &tv, time_t sec)
{
char buffer[30];
struct timeval ctv;
gettimeofday(&ctv, NULL);
if ( (ctv.tv_sec * 1000000 + ctv.tv_usec) >= ((tv.tv_sec + sec) * 1000000 + tv.tv_usec) )
{
gettimeofday(&tv, NULL);
//tv.tv_sec += sec;
//%m-%d-%Y
strftime(buffer,30,”%Y-%m-%d %T.”,localtime(&(tv.tv_sec)));
fprintf(stderr, “%s%ld\n”,buffer,tv.tv_usec);
return 1;
}
else
{ return 0;
}
}
void test_1()
{
struct timeval tv;
SetTimer(tv, TIMER_5_SECONDS);
fprintf(stderr, “start counting.\n”);
while (1)
{
if ( 1 == CheckTimer(tv, TIMER_5_SECONDS) )
{ fprintf(stderr, “Welcome to ……\n”);
}
}
}
int main(void)
{
test_1();
return 0;
}
###end
Welcome to mail me: txwcan999@gmail.com
Commented freepig on June 20th, 2008.the code is good; I’ve found a lot of similar examples on the net but no one indipendent by the current time/date. For example, in my application, timer run for a lot of time (hours or days) and in the meaning time it is appened that someone has changed the time of the system causing the timer to show an erroneous count (the same automatic legal/solar date change cause the problem).
Commented Gianluca on August 4th, 2008.Can you show an example on how obtain a timer base on system tick count intead on date/time?
thanks
Note: this program is not yet done…………
int xtime_make(struct xtime *xtp,
const struct tm *tmptr,
const timezone_t *tz)
{
if (tz == NULL) {
if (tmptr->tm_sec tm_sec > 60 ||
tmptr->tm_min tm_min > 59 ||
tmptr->tm_hour tm_hour > 23 ||
tmptr->tm_mon tm_mon > 11 ||
tmptr->tm_mday tm_mday > … ||
tmptr->tm_isdst > 0)
{
int mon = tmptr->tm_mon - 1;
int year = tmptr->tm_year + 1900;
int sec = tmptr->tm_sec;
if (!xtp) return 0;
if (mon nsec = 1_000_000_000;
Commented lourgdres on August 30th, 2008.sec = 59;
} else
xtp->nsec = 0;
xtp->sec = ((((int_fast64_t) (year/4 - year/100 + year/400 +
367*mon/12 + tmptr->tm_mday) +
year*365 - 719499) * 24 +
tmptr->tm_hour) * 60 +
tmptr->tm_min) * 60 +
sec;
// … check for negative years …
}
}
} else {
// handle other timezones
// …
}
}
Does anyone know how to make a C (i.e. non C++) timer?
Commented Jörgen on September 8th, 2008.very easy to learn and understand
Commented Avinash Gupta on September 14th, 2008.