Timer in C++

Hi Guys,

Is there any timer functionality in C++ or C?

Actually, what I am trying to do is in my network programming code, if a station is idle for certain amount of time, I need to delete the station's info from my list. That's why I need a timer.

Any suggestions?

Thanks in advance
sleep(int x);

should should work you could have a delete program waiting on the sleep timer and possibly stop the sleep if some one access the station before the sleep is dune

or perhaps get a time stamp from the computer save it and retrieve time stamps tell the desired one is reached but what ever works for you i suppose (lul kinda like making your own timer here though xD)
Last edited on
Thanks for the reply CrimsonAngel


sleep(int x);

should should work you could have a delete program waiting on the sleep timer and possibly stop the sleep if some one access the station before the sleep is dune


But wouldn't the program execution will be stopped if I use sleep?
I don't want other parts of my code to stop executing. It should run in parallel

And if I use time stamp then I will have to check it after every second. I don't want that, what I want is once I add an element into the list with it's timer set to say 50 sec., if someone calls that struct then timer will be reset to 50 sec., however if no-one calls that struct then after 50 sec., it will delete that struct from that list.


Last edited on
check this thread out http://www.cplusplus.com/forum/beginner/317/
this is what i meant xD

Last edited on
For all the time documentation read http://www.cplusplus.com/reference/clibrary/ctime/
sleep will kind of put your program at rest for int x milliseconds

i make a function wait(int seconds):
1
2
3
4
5
6
7
void wait(long seconds)
{
	seconds = seconds * 1000;
	Sleep(seconds);
}

wait(1); //waits for 1 second or 1000 milliseconds 
Last edited on
Look up the documentation on boost::timer and see if that fits your needs.
And if I use time stamp then I will have to check it after every second. I don't want that, what I want is once I add an element into the list with it's timer set to say 50 sec., if someone calls that struct then timer will be reset to 50 sec., however if no-one calls that struct then after 50 sec., it will delete that struct from that list.


There's nothing in the C or C++ Standard or libraries that will do that automatically for you.
Thanks for the reply guys.

There's nothing in the C or C++ Standard or libraries that will do that automatically for you.


I guess then I will have to implement my own timer.
I use the following to time things

1
2
3
4
5
6
7
8
9
10
11
#include <time.h>

clock_t init, final;

init=clock();
//
// do stuff
//
final=clock()-init;
cout << (double)final / ((double)CLOCKS_PER_SEC);


You could probably include init as part of your data structure.
Last edited on
Thanks turbozedd, I'll give it a shot.
Sleep isn't part of the c++ std is it? Isn't is specific to windows? Anyway, sleep only causes the current thread to sleep while all other threads continue to be "eligible" to execute.

I assume this is a multi-threaded program. Also, sleep is not going to be completely accurate. If you write Sleep(5000), your thread will go to sleep for a minimum of 5 seconds but not exactly 5 seconds. It means that after 5 seconds your thread can be selected to execute but it is only eligible to execute. The OS still has to perform a context switch from whatever was executing at that time. You could sleep for small chunks of time while keeping track of a timer so that after the thread awakens you can check the timer to see how much time has actually elapsed. then you can determine whether to call Sleep again or whether to destroy or reset the timer. I don't think that there is any kind of "unsleep" operation either. Once you call Sleep I don't think that there is a way to wake that thread up. Is this a windows program? Linux? Embedded? There may be better tools depending on your environment.
Sleep is a windows function and it kills a thread
http://www.cplusplus.com/forum/general/7745/
@Bazzy
I don't think that "kill" is the correct word. Sleep doesn't kill or terminate a thread. I'm not sure why you are using that word at all. Sleep causes the thread to stop executing for at least the time specified. Eventually the thread will be eligible to execute again.
I've tried that. after calling Sleep, the thread no longer executes. If you read the link I posted you will see that I came with that expression from there
Bazzy, that other forum discussion (that you posted) is about a single threaded program. Also, I never saw any resolution within that discussion. Anyway, I have used the windows provided Sleep function plenty during my lifetime and I can assure you that it does not "kill" a thread.

@kevinchkin
But wouldn't the program execution will be stopped if I use sleep?
I don't want other parts of my code to stop executing. It should run in parallel


The current thread will be stopped but other threads will continue to execute until the time is up. Once the specified time is up the thread that was put to sleep will be eligible to execute again. This is how the Sleep function works. Since you are talking about parallel processing your program must be multithreaded correct?
@Bazzy
I assume this is a multi-threaded program.


No dude, technically it's not a multi-threaded program, it's a client-server model. I am actually working on network emulator (Data-Link & Network Layer). Having said that, I don't see any point as to why it cannot be a multi-threaded program. So I guess I can use threads. However, I was more going towards something that does not uses threads.

Thanks
if a station is idle for certain amount of time, I need to delete the station's info from my list.
I think periodically checking the state of a station is the way to go, here.
@helios

I think periodically checking the state of a station is the way to go, here.


That's the thing, I am thinking that periodically checking won't be a very good idea because 1. It will be kind of a bad design that I have to go again and again just to check the timer of all the objects present.
2. Timer will be set to 60sec.

I think I would be using threads for that, as mentioned by Bazzy (in the posts above). I can't think of any other way right now.

If you have any other suggestions please let me know

Thanks
Last edited on
Topic archived. No new replies allowed.