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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:23:13+00:00 2026-05-30T20:23:13+00:00

My application is divided into some smaller domains and in order to reduce the

  • 0

My application is divided into some smaller domains and in order to reduce the dependencies between them I am going to use context object. Let’s consider a simple example:

class SomeType1;
class SomeType2;    
class dummy;

//context for first domain
class foo
{
public:
    virtual void setPtr1 (SomeType1* val) = 0;
    virtual SomeType2* getPtr2 () = 0;

    static foo* getCTX()
    {
        //statement bellow is a singleton which creates one instance of dummy and
        //returns its address as foo*
        return AppCTX::AccessorType<dummy>::getCTX<foo>();
    }
    virtual ~foo();
};

//context for second domain
class bar
{
public:
    virtual void setPtr2 (SomeType2* val) = 0;
    virtual SomeType1* getPtr1 () = 0;
    static bar* getCTX()
    {
         //same as above but casts dummy* to bar*
         return AppCTX::AccessorType<dummy>::getCTX<bar>();
    }
    virtual ~bar();
};

//dummy is a singleton created in AppCTX::AccessorType<dummy>
class dummy: public foo, public bar
{
public:
    virtual void setPtr1 (SomeType1* val)
    {
         ptr1 = val;
    }

    virtual SomeType1* getPtr1 ()
    {
         return ptr1;
    } 

    virtual void setPtr2 (SomeType2* val)
    {
         ptr2 = val;
    }

    virtual SomeType2* getPtr2 ()
    {
         return ptr2;
    } 
    virtual ~dummy();
private:
    SomeType1* ptr1;
    SomeType2* ptr2;
};

My domains starts multiple threads which use context also so ptr1 and ptr2 may be accessed in parallel. Do I need to synchronize setPtrs and getPtrs with mutexes? Is there any chance that ptr1 and ptr2 may get corrupted somehow?

  • 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-30T20:23:15+00:00Added an answer on May 30, 2026 at 8:23 pm

    It isn’t going to be very valuable to mutex your getPtr and setPtr functions. Even after the caller obtains the pointer and releases the mutex, it can still do whatever it wants to with the pointer it received. This could definitely cause problems with multiple threads.

    You actually want to put your locking mechanisms in the SomeType1 and SomeType2 classes themselves. For instance, you might acquire a mutex at the beginning of each member function of SomeType1 and then release the mutex before each member function returns.

    Some of your member functions may already be thread-safe. For instance, if they don’t access member variables or any other shared resources, then there isn’t any possibility of contention between threads. So, you wouldn’t have to mutex those. But you will need to look at each of your member functions and ask yourself what would happen if the member variables used by those functions changed unexpectedly. If the behavior would be undesirable, then that member function should be mutexed.

    But if what you are worried about is the actual pointer variables themselves getting corrupted, it depends on the machine architecture you are compiling for. For many systems (for which the assignment of a 32-bit pointer is an atomic operation) this won’t be an issue at all. That question is answered more fully here.

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

Sidebar

Related Questions

My PHP web application is divided into modules, and I use the data model
I have my ASP.NET MVC 2 application divided into few areas. One of them
I have an application that is divided into 4 quadrants, each quadrant is a
I have a login screen for a UITabBar application where users are divided into
I have an SDI application written in MFC. The frame is divided into 1
I would like to implement something like this because my application is divided into
I have an application that is currently divided into Service and Data Access Layers
we plan develop application that will, like many other modern applications is divided into
I am writing .NET3.5, WPF application using Composite Application Library. Application is divided into
I've an MVC application, which is divided into 3 layers: - Presentation - ASp.NET

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.