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 396271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:32:09+00:00 2026-05-12T16:32:09+00:00

Here is a skeleton of my thread class: class MyThread { public: virutal ~MyThread();

  • 0

Here is a skeleton of my thread class:

class MyThread {
public:
   virutal ~MyThread();

   // will start thread with svc() as thread entry point
   void start() = 0;        

   // derive class will specialize what the thread should do
   virtual void svc() = 0;                
};

Somewhere in code I create an instance of MyThread and later I want to destroy it.
In this case MyThread~MyThread() is called. MyThread:svc() is still running and using the object’s data members. So I need a way politely inform MyThread:svc() to stop spinning, before proceeding with the destructor.

What is the acceptable way to destroy the thread object?

Note: I’m looking for platform agnostic solution.

UPD: It’s clear that the root of problem is that there’s no relationship between C++ object representing thread and OS thread. So the question is: in context of object destuction, is there an acceptable way to make thread object behave like an ordinary C++ object or should it be treated as an unusual one (e.g. should we call join() before destoying it?

  • 1 1 Answer
  • 0 Views
  • 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-12T16:32:09+00:00Added an answer on May 12, 2026 at 4:32 pm

    Considering your additional requirements posted as comment to Checkers’ reply (which is the
    most straightforward way to do that):

    I agree that join in DTor is problematic for various reasons. But from that the lifetime of your thread object is unrelated to the lifetime of the OS thread object.


    First, you need to separate the data the thread uses from the thread object itself. They are distinct entities with distinct lifetime requirements.

    One approach is to make the data refcounted, and have any thread that wants to access it hold a strong reference to the data. This way, no thread will suddenly grab into the void, but the data will be destroyed as soon as noone touches it anymore.


    Second, about the thread object being destroyed when the thread joins:
    I am not sure if this is a good idea. The thread object is normally a way to query the state of a thread – but with a thread object that dies as soon as the thread finishes, noone can tell you wether the thread finished.

    Generally, I’d completely decouple the lifetime of the thread object from the lifetime of the OS thread: Destroying your thread object should not affect the thread itself. I see two basic approaches to this:

    1. Thread Handle Object – reference counted again, returned by thread creator, can be released as early as one likes without affecting the OS thread. It would expose methods such as Join, IsFinished, and can give access to the thread shared data.

    (If the thread object holds relevant execution state, the threafFunc itself could hold a reference to it, thereby ensuring the instance won’t be released before the thread ends)

    1. Thin Wrapper – You simply create a temporary around an OS thread handle. You could not hold additional state for the thread easily, but it might be just enough to make it work: At any place, you can turn an OS thread handle into an thread object. The majority of communication – e.g. telling the thread to terminate – would be via the shared data.

    For your code example, this means: separate the start() from the svc()

    You’d roughly work with this API (XxxxPtr could be e.g. boost::shared_ptr):

    class Thread
    {
       public:
         bool IsFinished();
         void Join();
         bool TryJoin(long timeout); 
    
         WorkerPtr GetWorker();
    
         static ThreadPtr Start(WorkerPtr worker); // creates the thread
    };
    
    
    class Worker
    {
    private:
       virtual void Svc() = 0;
    
       friend class Thread; // so thread can run Svc()
    }
    

    Worker could contain a ThreadPtr itself, giving you a guarantee that the thread object exists during execution of Svc(). If multiple threads are allowed to work on the same data, this would have to be a thread list. Otherwise, Thread::Start would have to reject Workers that are already associated with a thread.


    Motivation: What to do with rogue threads that block?
    Assuming a thread fails to terminate within time for one reason or another, even though you told it to. You simply have three choices:

    • Deadlock, your applicaiton hangs. That usually happens if you join in the destructor.
    • Violently terminate the thread. That’s potentially a violent termination of the app.
    • Let the thread run to completion on it’s own data – you can notify the user, who can safely save & exit. Or you simply let the rogue thread dance on it’s own copy of the data (not reference by the main thread anymore) until it completes.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the scenario: I'm writing an app that will watch for any changes
Take this skeleton class that someone wants to fill in to fetch RSS streams
Here's a skeleton Makefile just to make it easier to describe the problem: all_tests
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see

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.