Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timers

Status
Not open for further replies.

ankursaxena

Programmer
Sep 7, 2001
133
0
0
US
Hi! I want to know how exactly would you implement a timer in C++, which lets the current thread run and send a signal after a time out to the original thread so that the thread can do some work. just like an event i guess.
may be like

#define init 1
#define time_out 2

signal = init;
start_timer(sec); //this sets the signal to time_out

while(signal != time_out)
{
..
do other stuff that u might want to
}

do the stuff u wanted to do when the timer expired.

thanx a lot in advance..

actually better still if there are some good tutorials on this topic, it would be great.

thanx.
 
someone please help!!! dont tell me no one knows this..has to be some one..plzzz!!
 
One way is to use alarm() which sends a SIGALRM after a specified number of seconds.

#include <signal.h>
#include <unistd.h>

static volatile sig_atomic_t got_alarm;

static void
handler(int sig)
{
got_alarm=1;
}
int main()
{
signal(SIGALRM,handler);
/* Alarm will sound in 60 seconds */
alarm(60);
while (!got_alarm) {
/* Do stuff - until alarm */
}
return 0;
}

BTW, it isn't helpful (and a little rude, in my opinion) to post followup messages to your own post like you did above.

If anything, it probably tends to *prevent* responses because people will see that the thread count has increased and may not bother reading the thread, assuming the question has been answered.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top