I would probably not run the thread in the constructor, but would have a launch function. If the workflow is mostly invisible to the user, then this may not matter much, and the start in the constructor might be better. But if the user interacts with the workflow in some way (for example, their code runs in it), then in the end someone will need to create a certain state after creating the object, but before the thread starts. Murphy's law guarantees this; -)
I would stop it in the destructor and catch and register an exception. If the user probably needs to know the result of the stop (for example, if the failure means that the workflow may not have done its job), then there is also a stop function that they can request to get this information.
Btw, there are also some technical problems associated with starting a thread in the constructor in general. A new thread will potentially start before the constructor returns to the old thread. If he refers to his owner object (for example, to report on the results), he can therefore access the incompletely constructed object. It is usually easy to work, right up until the moment you inherit your original class. Now the thread starts before the constructor of the derived class starts, which can cause all kinds of problems. Therefore, if you start a stream in the constructor, and the stream can access the object directly or indirectly, take care and leave a lot of warnings.
source share