Start / stop thread in ctor / dtor or is it better to use start () / stop ()?

I have a class that internally uses a workflow. Currently, ctor starts a thread, and dtor stops (and waits) for it. Is this considered good code? I think it would be better to have separate start() / stop() functions for this purpose.

One of the problems is that stopping and waiting for a thread can throw exceptions, which is bad in dtor.

What would you advise me:

  • leave the code as it is and just catch and write exceptions in dtor
  • use start() / stop() , let the client handle the exceptions and just delete the stream in dtor (and give a warning about an unclean disconnect or something like that)
+4
source share
3 answers

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.

+4
source

I do not see both options as mutually exclusive. Here's how I do it:

 { mythread worker1; // starts mythread worker2(false); // doesn't start worker2.start(); worker1.stop(); } // dtor. of worker2 stops it, dtor. of worker1 does nothing 
+1
source

Depends a little on class semantics. If he needs an internal thread, it is good to start / stop the flow without outside interference.

It would be nice to use a thread pool, i.e. Reuse threads deleted by the previous instance of the class.

0
source

Source: https://habr.com/ru/post/1301323/


All Articles