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..is this possible in c++...

I suppose you could repeatedly call time() and exit the function when the desired amount of time has expired.

void foo()
{
   time_t t1, t2;
   t1 = t2 = time(); // starting time
   while( (t2 - t1) < 10 ) // 10 seconds
   {
       // do something
       t2 = time();
   }
}

Edited 5 Years Ago by Ancient Dragon: n/a

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();

Thanks for the reply..This is my code...Am working in VC++..As i told my timer will be within the function,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

void foo()
{
time_t t1, t2;
t1 = t2 = time(); // starting time
while( (t2 - t1) < 10 ) // 10 seconds
{
// do something

/* suppose here if it is waiting for the input or some external confirmation then how to solve this....*/


t2 = time();
}
}

Please check the code..I have edited your code so that you could understand the problem..

I suppose you could repeatedly call time() and exit the function when the desired amount of time has expired.

void foo()
{
   time_t t1, t2;
   t1 = t2 = time(); // starting time
   while( (t2 - t1) < 10 ) // 10 seconds
   {
       // do something
       t2 = time();
   }
}

Hi....I tried with your solution...But it won't work if function is waiting for an input..

You can use clock() to do this. You can find more about using clock() here. There's actually an exact example of what you want to do.

clock() is useless for his purpose. He needs a way to break out of input, such as gin.get(), after a certain time expires. There is no standard, or easy, way to do it. kumar may have to write his own custom keyboard input functions using os-specific api functions and threads.

Edited 5 Years Ago by Ancient Dragon: n/a

clock() is useless for his purpose. He needs a way to break out of input, such as gin.get(), after a certain time expires. There is no standard, or easy, way to do it. kumar may have to write his own custom keyboard input functions using os-specific api functions and threads.

Ah, yes I see. My bad. That is a tricky one, definitely feels like some multi-threading is going to be involved in the solution to that.

clock() is useless for his purpose. He needs a way to break out of input, such as gin.get(), after a certain time expires. There is no standard, or easy, way to do it. kumar may have to write his own custom keyboard input functions using os-specific api functions and threads.

Thanks for your suggestion...But I dont know much about threads as I am new to C++ language...Can you give me some more information that how can I use threads for this kind of problem

This article has been dead for over six months. Start a new discussion instead.