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

  • SEARCH
  • Home
  • 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 6136549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:37:00+00:00 2026-05-23T17:37:00+00:00

Suppose you have an object which can be accesed by many threads. A critical

  • 0

Suppose you have an object which can be accesed by many threads. A critical section is used to protect the sensitive areas. But what about the destructor? Even if I enter a critical section as soon as I enter the destructor, once the destructor has been called, is the object already invalidated?

My train of thought: Say I enter the destructor, and I have to wait on the critical section because some other thread is still using it. Once he is done, I can finish destroying the object. Does this make sense?

  • 1 1 Answer
  • 3 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-23T17:37:00+00:00Added an answer on May 23, 2026 at 5:37 pm

    In general, you should not destroy an object until you know that no other thread is using it. Period.

    Consider this scenario, based on your ‘train of thought’:

    • Thread A: Get object X reference
    • Thread A: Lock object X
    • Thread B: Get object X reference
    • Thread B: Block on object X lock
    • Thread A: Unlock object X
    • Thread B: Lock object X; unlock object X; destroy object X

    Now consider what happens if the timing is slightly different:

    • Thread A: Get object X reference
    • Thread B: Get object X reference
    • Thread B: Lock object X; unlock object X; destroy object X
    • Thread A: Lock object X – crash

    In short, object destruction must be synchronized somewhere other than the object itself. One common option is to use reference counting. Thread A will take a lock on the object reference itself, preventing the reference from being removed and the object being destroyed, until it manages to increment the reference count (keeping the object alive). Then thread B merely clears the reference and decrements the reference count. You can’t predict which thread will actually call the destructor, but it will be safe either way.

    The reference counting model can be implemented easily by using boost::shared_ptr or std::shared_ptr; the destructor will not run unless all shared_ptrs in all threads have been destroyed (or made to point elsewhere), so at the moment of destruction you know that the only pointer to the object remaining is the this pointer of the destructor itself.

    Note that when using shared_ptr, it’s important to prevent the original object reference from changing until you can capture a copy of it. Eg:

    std::shared_ptr<SomeObject> objref;
    Mutex objlock;
    
    void ok1() {
      objlock.lock();
      objref->dosomething(); // ok; reference is locked
      objlock.unlock();
    }
    
    void ok2() {
      std::shared_ptr<SomeObject> localref;
      objlock.lock();
      localref = objref;
      objlock.unlock();
    
      localref->dosomething(); // ok; local reference
    }
    
    void notok1() {
      objref->dosomething(); // not ok; reference may be modified
    }
    
    void notok2() {
      std::shared_ptr<SomeObject> localref = objref; // not ok; objref may be modified
      localref->dosomething();
    }
    

    Note that simultaneous reads on a shared_ptr is safe, so you can choose to use a read-write lock if it makes sense for your application.

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

Sidebar

Related Questions

Suppose I have an object in PHP $o, about which I can get $o->a->b,
Suppose I have a method which changes the state of an object, and fires
Suppose we have an object which represent a box. class Box { public: int
Suppose I have a Javascript object which is initialized var letters = {q:0, t:0,
Suppose I have an ExecutorService (which can be a thread pool, so there's concurrency
Say I want to have a global object which can be visible and accessible
Suppose I have a given object of type IEnumerable<string> which is the return value
Let's suppose I want to create a javascript class/object/function which have a method that
I have a Serializable object which is supposed to hold a java.awt.Image as its
Suppose I have an object file ( source.o ) without function main . a

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.