In general, C++ does not have a way to induce an exception to be thrown in code without having code that throws an exception in the code, or in code that code calls.
You could embed an ASL, a scripting language, or a separate process. All 3 could be designed to be interrupted (processes, for example, can be killed).
Boost has interruptable threads. How it works is that it has hooks in the boost synchronization primitives (mutexes etc), so when you interact with them it checks if your thread has been told to halt. If so, it then throws an exception.
An easy, partial solution is to
std::vector<std::future<R()>> futures;
futures.push_back( std::async( std::launch::async, []()->R{ /* code */ ) );
using std::chrono::literals;
if (futures.back().wait_for(500ms)==std::future_status::ready) {
auto r = futures.back().get();
futures.pop_back();
clear_ready_futures(futures); // wait for 0ms and if so, discard and destroy
return r;
}
// failed case
here our futures
stores the defunct futures (threads, in effect). clear_ready_futures
cleans any old ones that have finished.
Tasks that have started will still run to completion, stealing cpu, but the calling code does not have to wait for them.