How to make self-healing threads? (C ++)

I have a class with some function, for example:

void workerFunc(int ClassVariable) { boost::posix_time::seconds workTime(classVariableA); std::cout << "Worker: running" << std::endl; // Pretend to do something useful... boost::this_thread::sleep(workTime); std::cout << ClassVariable << std::endl; std::cout << "Worker: finished" << std::endl; } 

which I want to be in streams. and some other functions that I want to work like

 while(1) { boost::thread workerThread(workerFunc(ClassVariableB)); } 

therefore, it will create a stream whenever possible. But I need this thread to automatically destroy itself when it was finished. How to do it?

+4
source share
2 answers

You do not need to do anything for this. You just need to make sure that the thread really ends (i.e. there are no endless loops or such).

+5
source

The thread will automatically terminate when it finishes executing the function for which it was created.

join is a weird word, it really means wait_for, which means waiting for the thread to finish.

If you want to save a stream for reuse, it is usually implemented by creating its function loop every time it reaches a wait state, where it receives a warning (wakes up) when something waits. At some point, it will be a completion request or a request that will lead to its completion.

+2
source

All Articles