Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 603959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:59:13+00:00 2026-05-13T16:59:13+00:00

I’ve a class that internally uses a worker thread. Currently the ctor starts the

  • 0

I’ve a class that internally uses a worker thread. Currently the ctor starts the thread and the 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 the thread may throw exceptions, which is bad in a dtor.

What would you advice me:

  1. leave the code as it is and just catch and log exceptions in the dtor
  2. use start() / stop(), let the client handle exceptions and just delete the thread in the dtor (and issue a warning on unclean shutdown or something)
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:59:14+00:00Added an answer on May 13, 2026 at 4:59 pm

    I would probably not start the thread in the constructor, but rather have a start function. If the worker thread is basically invisible to the user then it might make little difference, and starting in the constructor might be better. But if the user interacts with the worker thread in any way (e.g. their code is run in it), then eventually someone will need to set up some state after the object is created, but before the thread runs. Murphy’s Law guarantees it 😉

    I would stop it in the destructor, and catch and log the exception. If the user is likely to need to know the result of stopping (for instance if failure means that the worker thread might not have done its work), then also have a stop function which they can optionally call in order to get that information.

    Btw, there are also some technical concerns to do with starting a thread in a constructor at all. The new thread will potentially run before the constructor returns in the old thread. If it accesses its owner object (for example to report the result), it can therefore access an incompletely-constructed object. That’s usually easy to work around, right up to the point where you inherit from your original class. Now the thread is started before the derived class constructor runs, which could cause all kinds of trouble. So if you do start a thread in a constructor, and the thread can access the object directly or indirectly, take care and leave lots of warnings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Looks like a server issue. Doesn't happen live. May 14, 2026 at 5:02 pm
  • Editorial Team
    Editorial Team added an answer Don't use parentheses to invoke the default constructor: vector<char> string1;… May 14, 2026 at 5:02 pm
  • Editorial Team
    Editorial Team added an answer You could write an extension method on List<T> which iterates… May 14, 2026 at 5:02 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.