Hi,

Actually, I want to make one Singleton class which will create one instance for one thread. I mean if we have 10 threads, then Singleton will create only one object for one thread. IF same thread will ask for one more instance it will return the same instance. Basically, my singeton is not process-specific, it would be thread-specific. Please tell how I can think for this modification of Singleton. I don't want the code but I need the idea how to proceed for the same. May be links or snippets will help. Thanks in advance.

Every thread has a thread id. http://www.cplusplus.com/reference/thread/thread/id/

You could have a manager that keeps track of thread-singleton objects created for each thread; if a thread asks for one, and that thread_id already has one, give back the one already made.

Use thread storage duration for the singleton.
http://en.cppreference.com/w/cpp/language/storage_duration

struct A // per-thread singleton
{
    static A& instance() ;

    private:
        A() { /* ... */ };
        A( const A& ) = delete ; // non-copyable
        A( A&& ) = delete ; // non-moveable
};

A& A::instance() // Meyer's singleton 
{
    thread_local static A singleton ; // one singleton per thread
    return singleton ;
}

http://coliru.stacked-crooked.com/a/64bc2a084ea4d294

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.