int global_count;
// this should not be changed, like its name, its a black box
void blackbox() {
while (true) {
printf(".");
global_count+=1;
}
}
int main()
{
int THREAD_NUM = 5;
for (int i=0; i<THREAD_NUM; i++) {
std::thread t(blackbox);
t.join(); // make thread t ONLY run for 5 seconds
}
return 0;
}
I know that ppl will say terminating a thread without return is dangerous and shouldn't be done. I also noticed there is a clean way, which is to use a condition variable and check it constantly, but this will need modification in the "blackbox()" - which can not be done in my case.
Is there any other workaround for such a problem? Can I call std::terminate() or std::abort() to stop the thread without memory leak?
thanks for the reply. The program I'm working on is written in C++ 11 and I'm just modifying it for a new feature. So I might not be able to use jthread? but it's interesting to know.
> If it is not restricted to thread, can I for example use process?
Instead of fumbling around in the dark, how about you tell us what the problem is, not your "solution" guesses. https://xyproblem.info/