[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    OT: thread safe singleton in pure C
From:       Stefan Kombrink <katakombi () gmail ! com>
Date:       2006-10-04 11:52:31
Message-ID: 200610041352.31282.katakombi () web ! de
[Download RAW message or body]

Hi all,


 I need to create a singleton class in pure C++, that needs to be thread safe.
I only need this one singleton in my environment (no polymorphy!)

I'd like to use this code which seems to fit for my needs:


class Singleton
{
    public:
        // get an instance
        static Singleton* getInstance()
        {
            static Singleton instance;
            return &instance;
        }

        // empty destructor, kills the static instance anyways
        ~Singleton() {}

    private:
        // constructor private
        Singleton() {}
        // protect copy operator
        Singleton(const Singleton& cc){}

....
....
....


Since my class needs to be thread safe I am wondering when exactly my static 
singleton instance is going to be created.
Is it - in any case - safe to call getInstance from different threads?
I took that code from 
http://de.wikipedia.org/wiki/Einzelstück_(Entwurfsmuster)#Implementierung_in_C.2B.2B
and it says that it's not thread safe.

Maybe some C++ expert could shed some light?

 thanks in advance, Stefan >8^)
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic