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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:17:04+00:00 2026-05-25T21:17:04+00:00

I have a class which can be created by multiple threads. But at one

  • 0

I have a class which can be created by multiple threads. But at one function the code needs to be protected, so I decided to use the boost interprocess mutex. Every class creates or opens the same Mutex in it’s constructor:

MyClass::MyClass()
{
       boost::interprocess::named_mutex m_Lock(
                 boost::interprocess::open_or_create, "myLock" );
}

So now there comes the point where the critical code part is called:

int MyClass::MyFunction()
{
       boost::interprocess::scoped_lock<boost::interprocess::named_mutex> lock(
                  m_Lock, boost::interprocess::try_to_lock);
       if(!lock)
       {
           return -1;
       }
       // else do some stuff here 
}

To clean up after the function ( and like its described on the boost page ) I use the remove command in my class destructor:

MyClass::~MyClass()
{
       boost::interprocess::named_mutex::remove("myLock");
}

Actually all this code works fine, but there is one concern I have:

As it is said in the description of the remove command :

Erases a named mutex from the system. Returns false on error. Never throws.

So that means the remove command just erases the Mutex out of the system – even if another thread has just locked it ( I tried this case already – it isn’t locked then anymore ).
So my problem is the following:
For example I have 3 Threads ( A, B and C ) – now the following happens:

  1. Process A creates an Instance of the class, calls the function and locks it
  2. Process B creates an Instances of the class, calls the function but can’t access code ( then waits e.g. )
  3. Process A finishes with the protected code and it gets unlocked
  4. Process B gains access to the protected code and locks it
  5. Process A deletes the Instance of the class -> the remove command is called
  6. Process C creates an Instance of the class, calls the function and can access the code since the remove command erased the Mutex –> Error!

So now someone may say ” Then don’t call remove! ” – Well is that possible? I mean since the named_mutex writes to the system I doubt it is erased without an explicit call, even if the program ends.
Anyone has some help?

  • 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-25T21:17:05+00:00Added an answer on May 25, 2026 at 9:17 pm

    From the boost docs, the remove call, is unnecessary. The destructor of named_mutex will automatically take care indicating to the OS that the process no longer needs the resource. You’re probably fine with just relying upon the built-in behavior of the destructor for cleanup.

    If you explicitly call remove, you’ll likely cause any other processes or threads attempting to use the named mutex to fail on any operations on the mutex. Depending on how your usage is orchestrated, this could either cause data races or crashing/exceptions being thrown in other processes.

    ~named_mutex();

    Destroys *this and indicates that the calling process is finished
    using the resource. The destructor function will deallocate any system
    resources allocated by the system for use by this process for this
    resource. The resource can still be opened again calling the open
    constructor overload. To erase the resource from the system use
    remove().

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

Sidebar

Related Questions

I am using C#. I have created a class which can be included in
I have a class Person which can have several Homes, each one with one
I have an instance of a class that can be accessed by multiple threads.
I have made a class which a form can inherit from and it handles
I have a class which contains a static collection that can be used across
I have a class which models all categories and they can be ordered hierarchically.
I have a class SomeClass, which can populate itself from a datarow in it's
I have a silly, little class FileSystemSize which can be used both as an
I have inherited a c# class 'Button' (which I can't change) which clashes with
I have a custom attribute which can be assigned to a class, [FooAttribute] .

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.