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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:31:20+00:00 2026-06-08T12:31:20+00:00

A design problem which I’ve seen in several workplaces I’ve been to, yet without

  • 0

A design problem which I’ve seen in several workplaces I’ve been to, yet without a satisfactory solution:

Suppose you have a system with dynamic number of threads.

Each thread must have an access to a set of “singletons”, the singletons have one and only one instance per thread (therefore they are not real singletons, but singletons per thread)

This set of singletons is known at compile time.

Each of the singletons has a default constructor (in order to simplify things, nevertheless, a solution that does not have this constrain would be great)

A satisfactory solution should have the followings:

  1. Each thread can access any of its singletons in o(1) time

  2. An access to a singleton is lock free

  3. Adding a singleton to the ‘singleton set’ does not require new code written on the set side

  4. The ‘singleton set’ is populated during compile time

I’m not sure if such a design is feasible. If it is, I assume that it requires a little bit of meta programming.

Thanks in advance for any of your insights.

  • 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-06-08T12:31:21+00:00Added an answer on June 8, 2026 at 12:31 pm

    Thread-local variables nicely solve the problem.

    // in .h
    class ThreadSingleton
    {
    private:
        static __thread ThreadSingleton* thread_specific_instance;
    
    public:
        static ThreadSingleton* get() { return thread_specific_instance; }
        ThreadSingleton();
        ~ThreadSingleton();
    };
    
    // in .cc
    __thread ThreadSingleton* ThreadSingleton::thread_specific_instance;
    
    ThreadSingleton::ThreadSingleton() {
        if(thread_specific_instance)
            std::abort(); // one instance per thread please
        thread_specific_instance = this;
    }
    
    ThreadSingleton::~ThreadSingleton() {
        thread_specific_instance = 0;
    }
    
    // usage
    int main() {
        // on thread entry
        ThreadSingleton x;
    
        // later anywhere in the thread
        ThreadSingleton* px = ThreadSingleton::get();
    }
    

    Each thread creates ThreadSingleton somewhere on the stack, normally in the thread function. Later on ThreadSingleton is accessible from anywhere in that thread via ThreadSingleton::get() which returns the singleton of the calling thread. (The above can be made a template to wrap any other class, I didn’t do it for simplicity of exposition).

    Performance-wise accessing thread-local variables doesn’t require any calls (unlike using thread-specific storage created using pthread_key_create) See http://www.akkadia.org/drepper/tls.pdf for more details.

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

Sidebar

Related Questions

We are developing an enterprise-size accounting system and we have logical design problem which
I have a database design problem which I have been researching for a while
I have a re-occurring design problem with certain classes which require one-off initialization with
I have a design problem which I would like some input on. Here are
The problem is mostly OOP design problem. I have a class which handles the
I have a design problem. I have two data objects which are instances of
I have a problem which is caused by our encapsulated design. Up till now
I have an interesting problem which I've been looking into and would appreciate some
I have design problem, which I would love to get a hand with. I
I have been confronted with a C++ design problem to choose between virtual function,

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.