jeudi 5 septembre 2019

Control thread execution time in async

I have an asynchronous process running (using std::async) which I want to measure the execution time and kill it if its taking too long. This process also returns a value after its execution, I would like to assign some default value as the result if it takes too long to compute. Any help/suggestions would be much appreciated!

#include <thread>
#include <future>

int compute(int val)
{
    int result;

    // do large computations

    return result;
}
void main()
{
    auto compute_thread = std::async(compute, 100);

    // TODO: wait for result only for x milliseconds else assign some default value
    int result = compute_thread.get();

    // resume sequential code.
    int final = result * 2;
}

Aucun commentaire:

Enregistrer un commentaire