4

I'm writing code in C++, and do an exhaustive search on the parameters. The problem is that for some kind of parameters, the function might goes into an infinite loop, and I can't control it (not my function, using it as a black box). My question is, can I run the function with a "time limit", so after, say 10 seconds, abort from the function and move for the next iteration?

for(int i=0; i < 100; i++){
   aBlackBoxFunction(i);
   /* This function may goes into a infinite loop :(
   I want that if it won't end after 10 seconds, the function would abort and move to the next iteration. I can't change the function itself */ 
}
  • 3
    Sure, just have your function record the time at which it started executing, and then periodically have it check the current time and subtract the start time from it. If the result of the subtraction is larger than some specified value, return. – Jeremy Friesner Aug 28 '15 at 06:44
  • That's a good idea, but again, I'm using this function as a black box, so if it goes into a infinite loop, it won't get to the next "check"... I can't add commands to that function :/ – AvivAbramovich Aug 28 '15 at 06:47
  • 3
    Start a new thread to run your function, and kill it when time limit exceeds. – Lingxi Aug 28 '15 at 06:52
  • @Lingxi that's actually sound like a good solution, can you add a sample code here? I never used threads in C++ – AvivAbramovich Aug 28 '15 at 06:55
  • @AvivAbramovich there is no real platform independent way do use threads in c++. – wimh Aug 28 '15 at 06:58
  • @Wimmel What about the standard thread support library introduced since C++11? Though it does not provide a kill method. – Lingxi Aug 28 '15 at 07:02
  • @Lingxi this question is not tagged c++11, so it is not clear if that can be used. – wimh Aug 28 '15 at 07:05
  • Can you use C++11? If so I have an answer for you. – rwols Aug 28 '15 at 07:12
  • 1
    @Wimmel: C++ has official ISO Standards, and thus official rules. Unless otherwise qualified (E.g. ISO/IEC 14882:1998) the current standard is implied. Currently that is ISO/IEC 14882:2014 aka C++14. C++11 actually means "not the new stuff". If you mean "just the stuff from the 20th century", tag it as C++98. – MSalters Aug 28 '15 at 09:51

3 Answers3

4

You can't do this safely. Realistically, you should expect mutexes to fail, and memory to leak. For this reason there is no standard C++ function.

Typically each OS does offer dedicated functions to kill a thread, but those should be used when the application is shutting down anyway, and you have stuck threads blocking a program exit. In those cases, there are no functioning threads anyway, and memory leaks no longer matter.

MSalters
  • 173,980
  • 10
  • 155
  • 350
1

A possible solution on the Windows platform. Use at your own risk :P (TerminateThread is inherently unsafe anyway).

#include <thread>
#include <windows.h>

template <typename Func>
bool time_limited_exec(Func func, DWORD time_limit) {
    std::thread thread(func);
    bool interrupted = false;
    if (WaitForSingleObjectEx(thread.native_handle(), time_limit, TRUE) != WAIT_OEBJECT_0) {
        interrupted = true;
        TerminateThread(thread.native_handle());
    }
    thread.join();
    return interrupted;
}
Lingxi
  • 14,579
  • 2
  • 37
  • 93
0

I would go ahead and implement a trivial event loop, and make the `time limited" function divide its work into chunks and periodically check if a stop event was not issued in the event loop after a timeout or another termination request.

This approach has a small overhead, but allows to interrupt work in the cleanest possible manner.

You can also use the event loop to send back progress made by the function, if you want to drive something like a progress bar to give the user a clue at the wait time.

Alternatively, you can implement a counter to track for infinite loop or recursion and simply return after a threshold has been exceeded. Or a timer and do work while time elapsed is less than 10 seconds.

The downside - any safe means of interrupting the function will have to be in it. So you will have to open up and hack that black box.

dtech
  • 47,916
  • 17
  • 112
  • 190