1

Suppose my main function calls an external function veryslow()

int main(){... veryslow();..}

Now I would like to the invocation part of very_slow in main, so that veryslow terminates if it runs out of a time bound. Something like this

int main(){... call_with_timeout(veryslow, 0.1);...}

What is a simple way to achieve that? My OS is Ubuntu 16.04.

1
  • Modify veryslow() to take a duration as a parameter, then bail from it when necessary. Commented Jun 5, 2018 at 3:24

3 Answers 3

3

You can call this function in a new thread, and set a timeout to terminate the thread, it will end this function call.

A POSIX example would be:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>

pthread_t tid;

// Your very slow function, it will finish running after 5 seconds, and print Exit message.
// But if we terminate the thread in 3 seconds, Exit message will not print.
void * veryslow(void *arg)
{
    fprintf(stdout, "Enter veryslow...\n");
    sleep(5);
    fprintf(stdout, "Exit veryslow...\n");

    return nullptr;
}

void alarm_handler(int a)
{
    fprintf(stdout, "Enter alarm_handler...\n");
    pthread_cancel(tid);    // terminate thread
}

int main()
{
    pthread_create(&tid, nullptr, veryslow, nullptr);

    signal(SIGALRM, alarm_handler);
    alarm(3);   // Run alarm_handler after 3 seconds, and terminate thread in it

    pthread_join(tid, nullptr); // Wait for thread finish

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

An idea, and a good one, but needs more detail to be a useful answer.
Yes, I didn't give actual code example because OP didn't specify the OS he is using. Thread related APIs are different in different OS.
1

You can use future with timeout.

std::future<int> future = std::async(std::launch::async, [](){ 
    veryslow();
});

std::future_status status;

status = future.wait_for(std::chrono::milliseconds(100));

if (status == std::future_status::timeout) {
    // verySlow() is not complete.
} else if (status == std::future_status::ready) {
    // verySlow() is complete.
    // Get result from future (if there's a need)
    auto ret = future.get();
}

Note that there's no built-in way to cancel an async task. You will have to implement that inside verySlow itself.

See here for more:

http://en.cppreference.com/w/cpp/thread/future/wait_for

6 Comments

"there's no built-in way to cancel an async task" So what's the benefit of using a future in this case ? That just makes it more complicated.
@SidS: Well, you are free to continue and ignore the invocation. And the calling thread doesn't block. Basically, you get all the benefits of async programming.
But it's still running. The question was how to make it stop running, after some time.
@SidS: The best idea would be to modify verySlow itself then. It's never a good idea to kill a thread, but if you insist, run verySlow in a thread, wait for timeout in calling thread, and then kill thread if it's still running.
I agree, the best idea is to modify veryslow(). No call for multithreading in this case.
|
0

i would pass a pointer to an interface into the function and ask for one back. with this i would enable two way communication to perform all necessary tasks--including timeout and timeout notification.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.