Need code to Exit function after 5 minutes.

Hi All,

Need your help.

I have a function which sometime takes more than 24 hours to run. So I want a code which will check the time and exit the function if the function executin is not successful and return to main function.

Thanks in advance.
Last edited on
Hi LB,

I have not looked into it. Is it something i need to download and use?

Could you please help me in understanding the same?

Update:
I had just gone through the links provided above and the examples for system_clock and steady_clock is used to get the time duration at the end.

My requirement is, if the function execution is not completed, after 5 minutes it has to exit the function and return to the main function and proceed with other part of the code..
Last edited on
It's part of the c++ 2011 standard. Just add #include <chrono> to use it.
@op:

You have 2 basic options here.

If you are doing this from 1 thread, then the long-running function is going to have to periodically interrupt what it's doing to check the time... and if the time has exceeded, it can do a normal return; to go back to main. If the function is doing something in a big loop, this is relatively easy to accomplish... just check the time every loop iteration.


If you want to spawn a separate thread to do this, then main can launch a new thread and simply wait for it to complete, while periodically polling the time. If the timeout occurs but the other thread hasn't finished it's job yet, you can forcibly close it. Note this might have some side-effects as forcibly closing a thread may not release all resources that thread uses (dtors and stuff aren't necessarily called).



I'd wager that the single-thread approach is probably easiest for you, in which case, the others' advice about the chrono header is what I'd recommend. But I can't say for sure without knowing the details of the problem.
Hi Disch,

I have created a thread and while terminating the thread there were some memory leaks and hence the program was abruptly closing with fatal error.

You can see what i used in this link.

http://www.cplusplus.com/forum/general/141527/

And about using loops to detrmine the time taken by the function,

I am using a tool which has few exposed APIs. In that sometimes one of the API is taking too much time to execute.

So if I use a loop, then without executing that API, it will not go to next iteration...! So i need something to stop execution of the function after 5 minutes.
If you're using an API that has a blocking call, then the only way to stop it would be to kill the thread. But that is pretty sloppy and could have side-effects like what you're seeing (killing a thread forcefully is not really a good idea).

Can I ask what API you're using that has a blocking API which takes several hours to complete? That is crazy. Maybe you should consider not using it and finding a more reasonable alternative.
The API that I am using is specific to the tool i am using and is not related to general C/C++ programming.

Yes i am facing lot of issues related to memory after killing the thread. But I want the to understand the reason why they have given TerminateThread or CloseHandle calls when using it will create such problems?
Maybe they figured it was better than having the program deadlock?



Another thing you can try is to launch a separate process and do inter-process communication to get the results. If the sub-process times out, you can kill it. Since it's a separate process and not just a thread, it'll wipe out all in-use memory and free all used resources.

Of course communicating between 2 processes is more work than communicating between 2 threads.
Hey,
Could you please give me small programming example on using separate process and killing one after time out?
I'm not good in CPP

Thanks.
Topic archived. No new replies allowed.