Reputation:
I have this design. I could not achieve what I need:
My goal is restart(re-run) to "worker" thread when it has finished its task. I intend to call the worker thread destructor for this approach because, it takes memory from heap. I need to clean-up all the related variables.
How can I achieve this?
int main()
{
// re-start start
A* worker = B::getInstance();
worker->start();
// re-start end
return a.exec();
}
Writing a for-loop is not a solution. Because, I need to learn worker thread is stopped completely. I tried using worker->wait(). But it does not work.
Upvotes: 1
Views: 2037
Reputation: 753475
The general rule in RAII (Resource Acquisition Is Initialization) is that the code that acquires a resource is responsible for ensuring it is released. Thus, your worker thread should terminate after releasing all resources it has acquired.
The general style of the main program will be:
Alternatively, the main thread can still run in the forever loop, but recognizes when a delegatable task has arrived and at that point, initialize the worker thread and give it the specific task. This allows for as many worker threads as there are incomplete delegatable tasks. You might need to throttle the number so that you don't have too many incomplete tasks.
Upvotes: 0
Reputation: 143765
I don't fully understand your question, but if you allocate the thread and you have to restart it again once it's completed, you can do the following:
to check:
to restart:
Upvotes: 0