Timer Problems

Hi...I want to create a timer so that after completing the time(suppose 10 sec) the control should come out of the function..Please note that am starting the timer inside the function.Code is given below..I want to give certain time limit to that function so that after completing the time the control should come out of the function..I don't want to calculate the time..I want to give my own time so that the function should complete its execution within that time period..suppose if function is waiting for an input then also after completing time limit the control should come out indicating that "time has expired"..once it comes out of the function then it should continue with the next function execution...Is it possible.is this possible in c++...
Begin();

// here I would like to add timer.

v_CallId = v_CallId1;
call_setup_ind();
call_alert_ind();
dir_read_search_cnf();
dir_save_cnf();

END();
Suppose the function is in the middle of doing something and has created resources to do that job? What do you expect to happen? Even cleaning up resources takes time.

I just want you think about it for a while.
Hi...Thanks for the reply...My work is to just complete the task within the prescribed time..I dont want very accuracy in time limit..I can give more time limit..That is not an issue...I just want control come out of the function if time expires...Is it possible..I thought of doing in multi-thread...Is it good one...If so how to do it...Am a fresher to c++...So I dont know much about Timer and its application.
There isn't a general way to do this. And it's not a typical non-realtime thing to do. The approach would be dictated by your actual requirements.

You still haven't said what you expect to happen if you run out of time. Do you abort, do you get a chance to revisit the task, ...
1
2
3
4
5
6
7
8
9
10
11
//start the function here

time_t startTime; //start the timer
do{ //time checking loop
  time_t currentTime; //get the current time
  if(currentTime - startTime >= 10){ //if 10 seconds passed...
    break; //break out of the loop
  } //if not...
} //check again!

//exit the function here 


Is that what you're trying to accomplish?
Hi programmer47...Your code is somewhat simillar...But I want one clarification...In the given code
if(currentTime - startTime >= 10){ //if 10 seconds passed...

//Suppopse here if it is waiting for an input,then howill it come out of the loop..

break; //break out of the loop
Is the program waiting for input while the 10 seconds are counting or after the time is up? Wouldn't you just use ' break; ' to come out of the loop if input is detected? It all depends on your method of input. This would be easy to implement if the code just checked the keyboard buffer, and if nothing was pressed, moved on to the next line of code.
fun()
{
//timer starts here (Say 10 sec of time limit )
fun2()
{
//suppose here it is waiting for an input
fun3()
{
//Here also it is waiting for an input
}
}
}

Think this is my code...Suppose fun2 is waiting for an input then also control should come out of the fun2 once the timer elapsed...Is it possible
Topic archived. No new replies allowed.