I would like to have a singleton class along the lines of the Meyer's Singleton but which is private to a single thread. The singleton should automatically destroy itself when the thread dies.
Any suggestions are appreciated!
Harald
I would like to have a singleton class along the lines of the Meyer's Singleton but which is private to a single thread. The singleton should automatically destroy itself when the thread dies.
Any suggestions are appreciated!
Harald
Make the Singleton a member of your thread class
From the Design Patterns book (Gamma, Helm, Johnson, Vlissides):
SINGLETON
-------------------
Intent
Ensure a class has only one instance, and provide a global point of access to it.
As a singleton is globally unique, I assume that what you require would simply be an instance of your object inside the thread class (that isn't based on the Singleton design pattern).
Regards,
Adam
I would like the class to have only one instance per thread.
There should be a global access to this instance within the thread.
Harald
The Singleton object instance is globally unique for the class you are making a Singleton.
By nature it had a single instance per application.
You would have to create several duplicate objects to achieve what I think you're after.
You could however have multiple objects stored in a static map within your "Singleton" and access these via some sort of thread ID.
For example:
class SingletonClass
{
public:
static void CreateInstance( unsigned int uiThreadID );
static SingletonClass* Instance( unsigned int uiThreadID );
static void DestroyInstance( unsigned int uiThreadID);
// other stuffs...
private:
static std::map<unsigned int, SingletonClass*> m_smSingletonMap;
// other stuffs...
};
However, you'd need to create and kill the object manually on thread creation and death respectively.
I still think you'd be better served by a single object instance as part of your thread class.
Adam.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.