C++ timer needed

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Is there a built in library or function for a timer in C++? I want to have something that can wait x number of seconds, call a function, and be reset.

Any help would be :cool:
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
It looks like I will need to come up with a way to have a timer in another thread or something. I can't have it blocking forever.

Right now I have:

bool = true;
while(bool)
{
//wait 60 seconds
//do work
}

I set that bool to false when I get another input, but the program stays in this while loop forever.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Why can't you have it blocking? Does the program have to do something in the background?
 

beyonddc

Senior member
May 17, 2001
910
0
76
In the class where the loop is located, add a function named something like "void stopLooping();"
And have another class to spawn a thread where the loop is located, then at certain time, you can invoke the stopLooping() function on it.
 

OCedHrt

Senior member
Oct 4, 2002
613
0
0
clock_t start = clock();
while(bool && (double) (clock() - start) / CLK_TCK < 60)
{
// do work
}
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: singh
Why can't you have it blocking? Does the program have to do something in the background?

Yes. It still needs to read messages from a socket and process them.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
I'm confused. So one thread has sockets reading messages and the other one is waiting?! Waiting for what?
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
do a blocking read on the socket, and a sleep on the other thread.
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: xtknight
I'm confused. So one thread has sockets reading messages and the other one is waiting?! Waiting for what?

I have a tthread that reads messages from a socket a nd processes them. If a certain message Id is encountered I need send a certain response message every 60 seconds. So the main thread has to keep processing messages while the timer is going.