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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:33:47+00:00 2026-05-13T05:33:47+00:00

I know, I know, the title of my message may seem provocative, since boost::mutex

  • 0

I know, I know, the title of my message may seem provocative, since
boost::mutex purposefuly do not expose lock / unlock (in order to avoid dead locks).

However the boost documentation is quite short on these aspects (to say the least), so I am asking if anyone can help me in the following use case.

Suppose you have a class Foo, which has :
– a destructor that takes some time to complete
– a method that is called by a distinct thread, but should not be called during destruction

class Foo
{
 public:

  virtual ~Foo()
  { 
    //Time consuming operations here
  }


  //Method called by a timer belonging to a distinct class 
  void OnTimer()
  {
    //Other time consuming stuff. Should not be called during destruction !
  }  


}; 

I tried (with no success) to implement a version based on boost::mutex

//boost::mutex implementation
class Foo
{
public:
  Foo()
  {
  }

  virtual ~Foo()
  { 
    {
      boost::mutex::scoped_lock lock(mDisposingMutex);
      //Time consuming operations here
    }
  }


  //Method called by a timer belonging to a distinct class 
  void OnTimer()
  {
    {
      //Imaginary code here: mutex::locked() method is private !!!
      if ( ! mDisposingMutex.locked()) 
        return;
    }    
    //Other time consuming stuff. Should not be called during destruction !
  }  

private:
  boost::mutex mDisposingMutex; 
}; 

Am I totally wrong? Can anyone tell me how this is supposed to be done with boost::mutex?

Thanks !

  • 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-13T05:33:48+00:00Added an answer on May 13, 2026 at 5:33 am

    If you do commit to using Lockable::lock() in the destructor body, you could have your OnTimer() function use Lockable::try_lock(), and proceed only if that function returns true. That will have OnTimer() put the destructor on hold if OnTimer() starts first, but it still doesn’t solve the problem of the destructor running, finishing and releasing the mutex, and then OnTimer() starting and successfully grabbing the mutex.

    Such a sequence is likely in the realm of undefined behavior, but that curse won’t stop it from happening. Using a state flag in addition to the mutex — similar to what I described in my comment above — could allow you to detect this latter case and stop OnTimer() from doing anything beyond reading the flag. At some point, though, this is just putting Band-Aids on top of Band-Aids.

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

Sidebar

Related Questions

This title may seem strange, so let me try to explain what I'm trying
I know this question may seem repetitive as compared to a few other questions
I know the title is somewhat confusing, my problem is this: I want to
I know the title makes this sound very easy, but I have a For
I know the title is ridiculously long, but I'm in need of some assistance
I know the title isn't the best, I didn't know how to put this
I know the title of the question sounds absolutely weird but I had no
I know the title isn't very clear. I'm new to PHP, so there might
I know the title is a little off, but it's hard to explain the
I know the title didnt say much, but here I go with a clearer

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.