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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:00:13+00:00 2026-05-13T15:00:13+00:00

I have a class that is shared between several projects, some uses of it

  • 0

I have a class that is shared between several projects, some uses of it are single-threaded and some are multi-threaded. The single-threaded users don’t want the overhead of mutex locking, and the multi-threaded users don’t want to do their own locking and want to be able to optionally run in “single-threaded mode.” So I would like to be able to select between real and “dummy” mutexes at runtime.

Ideally, I would have a shared_ptr<something> and assign either a real or fake mutex object. I would then “lock” this without regard to what’s in it.

unique_lock<something> guard(*mutex);
... critical section ...

Now there is a signals2::dummy_mutex but it does not share a common base class with boost::mutex.

So, what’s an elegant way to select between a real mutex and a dummy mutex (either the one in signals2 or something else) without making the lock/guard code more complicated than the example above?

And, before you point out the alternatives:

  • I could select an implementation at compile time, but preprocessor macros are ugly and maintaining project configurations is painful for us.
  • Users of the class in a multi-threaded environment do not want to take on the responsibility of locking the use of the class rather than having the class do its own locking internally.
  • There are too many APIs and existing usages involved for a “thread-safe wrapper” to be a practical solution.
  • 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-13T15:00:13+00:00Added an answer on May 13, 2026 at 3:00 pm

    How about something like this?
    Its untested but should be close to OK.
    You might consider making the template class hold a value rather than a pointer
    if your mutexes support the right kinds of constructions. Otherwise you could specialise the MyMutex class to get value behaviour.

    Also it’s not being careful about copying or destruction .. I leave that as an exercise to the reader 😉 ( shared_ptr or storing a value rather than a pointer should fix this)

    Oh and the code would be nicer using RAII rather than explicit lock/unlock… but that’s a different question.I assume thats what the unique_lock in your code does?

    struct IMutex
    {
      virtual ~IMutex(){}
      virtual void lock()=0;
      virtual bool try_lock()=0;
      virtual void unlock()=0;
    };
    
    template<typename T>
    class MyMutex : public IMutex
    {
      public:
        MyMutex(T t) : t_(t) {}
        void lock() { t_->lock(); }
        bool try_lock() { return t_->try_lock(); }
        void unlock() { t_->unlock(); }
      protected:
        T* t_;
    };
    
    IMutex * createMutex()
    {
      if( isMultithreaded() )
      {
         return new MyMutex<boost::mutex>( new boost::mutex );
      }
      else
      {
         return new MyMutex<signal2::dummy_mutex>( new signal2::dummy_mutex );
      }
    }
    
    
    int main()
    {
       IMutex * mutex = createMutex();
       ...
       {
         unique_lock<IMutex> guard( *mutex );
         ...
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class library that is shared between multiple Role projects in my
I have a class containing an ExecutorService that can be shared between threads: class
I have a class that defines a transaction that needs to be shared between
I have a utility class that creates & returns a db connection: Public Shared
I have several divs that share a common class. If one of these divs
I have class that represents users. Users are divided into two groups with different
I'm cleaning up some code, and I have a class with an entity that
I'm implimenting my own ApplicationContext class that uses the singleton pattern. I want to
I have a ValidationAttribute that I have created which is shared between the Server,
I have a class that contains a few private/protected fields and some public getters

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.