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:
-
Each thread can access any of its singletons in o(1) time
-
An access to a singleton is lock free
-
Adding a singleton to the ‘singleton set’ does not require new code written on the set side
-
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.
Thread-local variables nicely solve the problem.
Each thread creates
ThreadSingletonsomewhere on the stack, normally in the thread function. Later onThreadSingletonis accessible from anywhere in that thread viaThreadSingleton::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.