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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:37:46+00:00 2026-05-11T16:37:46+00:00

I’ve run into a problem which seems troubling to me. It seems I’ve found

  • 0

I’ve run into a problem which seems troubling to me. It seems I’ve found a situation that’s easy enough to work-around, but that could lead to problems if a) I have a lapse in concentration while programming or b) somebody else starts implementing my interfaces and doesn’t know how to handle this situation.

Here’s my basic setup:

I’ve got an abstract class that I’m using as a generic interface to several data types. I’ve adopted the non-virtual public interface paradigm (Sutter, 2001) along with scoped locking to provide some thread safety. An example interface class would look something like this (I’ve left out details about scoped locking and the mutex implementation, as I don’t think they’re relevant):

class Foo
{
public:
    A( )
    {
        ScopedLock lock( mutex );
        aImp( );
    }
    B( )
    {
        ScopedLock lock( mutex );
        bImp( );
    }
protected:
    aImp( ) = 0;
    bImp( ) = 0;
}

It is then up to the user to implement aImp and bImp, which is where the problem comes in. If aImp performs some operation which uses bImp, it’s extremely easy (and almost logical, in some sense) to do this:

class Bar
{
protected:
    aImp( )
    {
        ...
        B( );
        ...
    }
    bImp( )
    {
        ...
    }
}

Deadlock. Of course, the easy solution to this is to always call the protected virtual functions rather than their public variants (replace B( ) with bImp( ) in the above snippet). But it still seems far to easy to hang myself if I make a mistake, or worse yet allow others to hang themselves.

Does anybody have some way to attempt to either stop an implementer of the abstract class from calling those public functions at compile-time, or otherwise help to avoid the deadlock solution?

Just for kicks, some mutexes allow for operation which will avoid deadlock problems. As an example, if I implement this using the windows functions EnterCriticalSection and LeaveCriticalSection, there’s no issue. But I’d rather avoid platform specific functionality. I’m currently using boost::mutex and boost::shared_mutex in my scoped lock implementation, and as far as I’ve seen it doesn’t attempt to avoid deadlock (which I think I almost prefer).

  • 1 1 Answer
  • 1 View
  • 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-11T16:37:47+00:00Added an answer on May 11, 2026 at 4:37 pm

    Using private inheritance will potentially solve your problem:

    class Foo
    {
    public:
      void A( )
        {
          ScopedLock lock( mutex );
          aImp( );
        }
      void B( )
        {
          ScopedLock lock( mutex );
          bImp( );
        }
    
    protected:
      virtual void aImp( ) = 0;
      virtual void bImp( ) = 0;
    };
    
    class FooMiddle : private Foo
    {
    public:
      using Foo::aImp;
      using Foo::bImp;
    };
    
    class Bar : public FooMiddle
    {
      virtual void aImpl ()
      {
        bImp ();
        B ();                   // Compile error - B is private
      }
    };
    

    Deriving from Foo privately, and then using FooMiddle ensures that Bar doesn’t have access to A or B. However, bar is still able to override aImp and bImp, and the using declarations in FooMiddle mean that these can still be called from Bar.

    Alternatively, an option that will help but not solve the problem is to use the Pimpl pattern. You’d end up with something as follows:

    class FooImpl
    {
    public:
      virtual void aImp( ) = 0;
      virtual void bImp( ) = 0;
    };
    
    class Foo
    {
    public:
      void A( )
        {
          ScopedLock lock( mutex );
          m_impl->aImp( );
        }
      void B( )
        {
          ScopedLock lock( mutex );
          m_impl->bImp( );
        }
    
    private:
      FooImpl * m_impl;
    }
    

    The benefit is that in the classes deriving from FooImpl, they no longer have a “Foo” object and so cannot easily call “A” or “B”.

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

Sidebar

Related Questions

No related questions found

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.