First, do not kill the losing algorithm. Just let it run to the end and ignore the result.
Now, the closest thing to what you requested is to have the variable mutex + condvar + result (or, more likely, two results, one for each algorithm).
The code would look like
X result1, result2; bool complete1 = false; bool complete2 = false; std::mutex result_mutex; std::condition_variable result_cv; // simple wrapper to signal when algoN has finished std::thread t1([&]() { result1 = algo1(); std::unique_lock lock(result_mutex); complete1 = true; result_cv.notify_one(); }); std::thread t2([&]() { result2 = algo2(); std::unique_lock lock(result_mutex); complete2 = true; result_cv.notify_one(); }); t1.detach(); t2.detach(); // wait until one of the algos has completed int winner; { std::unique_lock lock(result_mutex); result_cv.wait(lock, [&]() { return complete1 || complete2; }); if (complete1) winner=1; else winner=2; }
Other mechanisms, including future / promise, require the main thread to be busy. The only alternative without waiting for a wait is to move the processing after success to call_once : in this case, the main thread should just join both children, and the second child will simply return when it finishes processing and realizes that it is lost.
source share