I have several work functions, that call accessory functions that might have something bad happen. I want to be able to stop the work functions if an accessory function determines something bad happened, without putting numerous flag checks in the work functions. For example,
struct Worker {
bool badhappened = false;
Worker() {
std::thread([&]() {
while ( not badhappened );
// kill the work
}).detach();
}
int WorkComponent {
if ( badhappening() )
badhappened = true;
return someint;
}
void DoWork {
// WorkComponents called several times
}
}
But I don't know what to call at kill the work
. It's not a problem for DoWork
to happen in a separate thread, but there doesn't appear to be an equivalent pthread_kill
in C++'s threads. Is there a solution that doesn't involve putting several if(badhappened) return;
calls into DoWork
?