can we force a c++ program to end after a certain amount of time
i want to force my program to quit after a certain amount of time so that it would not go on for ever..... :)

Recommended Answers

Answered by Narue 5,707 in a post from
Answered by twomers 408 in a post from

Probably a good idea.

Jump to Post

All 5 Replies

Yes.

Probably a good idea.

thanks for the quick reply
how can we do that??

use the time() function if you only want accuracy to the second, of clock() for milliseconds.

const time_t MaxTime = 60*60; // one minute timer
time_t t1 = time(0); // start the timer
bool done = false;
while( !done )
{
    time_t t2 = time(0); // get current time
    if( (t2-t1) >= maxtime)
        done = true;
    // do some processing here
}

Or you could create another thread that does the timer stuff. When time is expired the timer thread tells the main thread to quit.

thanks Dragon i will check it out and then revert back to u :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.