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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:44:29+00:00 2026-05-31T19:44:29+00:00

There is a sample code which works in a multithreading environment: void CSampleClass::Stop(void) {

  • 0

There is a sample code which “works” in a multithreading environment:

void CSampleClass::Stop(void) {
  if (m_pDB != nullptr) {
  ... Here is some code
    m_pDB->Interrupt();
  }
}

, where the m_pDB member is declared as boost::shared_ptr<CSampleDatabase> m_pDB;. m_pDB can be reset in another class method. That is why it is tested to not nullptr. As the code have multiple threads, can be a situation when m_pDB is reset, by another thread, in between if (...) and m_pDB->Interrupt();. The result is quite dramatic. To prevent such situation I use the following code modification

void CSampleClass::Stop(void) {
  auto pDb = m_pDB; //lock
  if (pDb != nullptr) {
  ... Here is some code
    pDb->Interrupt();
  }
}

, i.e. if m_pDB.reset(); called, the object is never released until pDb is destroyed.

The questions are:

  1. Is there a “standard” way to prevent the situation without involving lockers, mutexes, critical sections, etc.? Something like using boost::weak_ptr to break circular references.

  2. Is it guaranteed that the compiler declares pDB as boost::shared_ptr<CSampleDatabase> and not CSampleDatabase *? May be it is safer to write decltype(m_pDB) pDb = m_pDB; //lock?

  • 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-31T19:44:30+00:00Added an answer on May 31, 2026 at 7:44 pm

    The solution presented is not safe. It is similar to this example from the documentation:

    // thread A
    p = p3; // reads p3, writes p
    
    // thread B
    p3.reset(); // writes p3; undefined, simultaneous read/write
    

    Simultaneous reads from the same instance are safe. Any other concurrent operations on the same instance are not.

    Is there a “standard” way to prevent the situation without involving lockers, mutexes, critical sections, etc.? Something like using boost::weak_ptr to break circular references.

    You need mutual exclusion, so you need to use a mutual exclusion construct.

    Is it guaranteed that the compiler declares pDB as boost::shared_ptr<CSampleDatabase> and not CSampleDatabase *? May be it is safer to write decltype(m_pDB) pDb = m_pDB; //lock?

    Yes, pDB will be a shared_ptr.

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

Sidebar

Related Questions

There is a sample code of apple named reachability which tells us the network
I have downloaded an sample code, so there are some CString variables in that
In the Apple documentation for NSRunLoop there is sample code demonstrating suspending execution while
Is there any utility (or sample source code) that truncates HTML (for preview) in
I have the following piece of code which works fine and does what it
All, I am studying some sample code given in my web dev class as
I am porting some Unix code to Windows which redirects stderr and stdout to
Have some Perl code which is using the DBI module - (the code is
Below imagemagick code which works fine by creating RUb with the background color black
I have C# code to populate a dropdown list in Silverlight which works fine

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.