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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:51:25+00:00 2026-05-16T00:51:25+00:00

I have an object that is called from two different threads and after it

  • 0

I have an object that is called from two different threads and after it was called by both it destroys itself by “delete this”.

How do I implement this thread-safe? Thread-safe means that the object never destroys itself exactly one time (it must destroys itself after the second callback).

I created some example code:

class IThreadCallBack                                                                                             
{                                                                                                                 
  virtual void CallBack(int) = 0;                                                                                 
};                                                                                                                

class M: public IThreadCallBack                                                                                   
{                                                                                                                 
private:                                                                                                          
  bool t1_finished, t2_finished;                                                                                  

public:                                                                                                           

  M(): t1_finished(false), t2_finished(false)                                                                     
  {                                                                                                               
    startMyThread(this, 1);                                                                                       
    startMyThread(this, 2);                                                                                       
  }                                                                                                               

  void CallBack(int id)                                                                                           
  {                                                                                                               
    if (id == 1)                                                                                                  
    {                                                                                                             
      t1_finished = true;                                                                                         
    }                                                                                                             
    else                                                                                                          
    {                                                                                                             
      t2_finished = true;                                                                                         
    }                                                                                                             

    if (t1_finished && t2_finished)                                                                               
    {                                                                                                             
      delete this;                                                                                                
    }                                                                                                             
  }                                                                                                               
};                                                                                                                

int main(int argc, char **argv) {                                                                                 
  M* MObj = new M();                                                                                              
  while(true);                                                                                                    
}                                                                                                                 

Obviously I can’t use a Mutex as member of the object and lock the delete, because this would also delete the Mutex. On the other hand, if I set a “toBeDeleted”-flag inside a mutex-protected area, where the finised-flag is set, I feel unsure if there are situations possible where the object isnt deleted at all.
Note that the thread-implementation makes sure that the callback method is called exactly one time per thread in any case.

Edit / Update:
What if I change Callback(..) to:

void CallBack(int id)

{    
  mMutex.Obtain()
  if (id == 1)
  {
    t1_finished = true;
  }
  else
  {
    t2_finished = true;
  }
  bool both_finished = (t1_finished && t2_finished);

  mMutex.Release();

  if (both_finished)
  {
    delete this;     
  }
}

Can this considered to be safe? (with mMutex being a member of the m class?)

I think it is, if I don’t access any member after releasing the mutex?!

  • 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-16T00:51:26+00:00Added an answer on May 16, 2026 at 12:51 am

    Use Boost’s Smart Pointer. It handles this automatically; your object won’t have to delete itself, and it is thread safe.

    Edit:
    From the code you’ve posted above, I can’t really say, need more info. But you could do it like this: each thread has a shared_ptr object and when the callback is called, you call shared_ptr::reset(). The last reset will delete M. Each shared_ptr could be stored with thread local storeage in each thread. So in essence, each thread is responsible for its own shared_ptr.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Summary The problem is that field is not a good… May 16, 2026 at 3:11 am
  • Editorial Team
    Editorial Team added an answer NSNumberFormatter class has built-in functionality for conversion numbers to words:… May 16, 2026 at 3:11 am
  • Editorial Team
    Editorial Team added an answer If you're determined to avoid threads, I would suggest using… May 16, 2026 at 3:11 am

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.