#include <iostream>

using namespace std;

 

class Mutex {

public:

        void lock() {

               cout << "Mutex::lock " << endl;

        }

 

        void unlock() {

               cout << "Mutex::unlock " << endl;

        }

};

 

class Cursor {

private:

        Cursor() {}

        Cursor(const Cursor& c);

        Cursor& operator=(const Cursor& c);

 

        static Cursor* sInstance;

        static Mutex mutex;

 

public:

        static Cursor& getInstance() {

               //-------------------------

               mutex.lock();

               if(sInstance==0)

                       sInstance=new Cursor;

               mutex.unlock();

               //-------------------------

               return *sInstance;

        }

};

 

Cursor* Cursor::sInstance;

Mutex Cursor::mutex;

 

int main(void) {

        Cursor& c1 = Cursor::getInstance();

        Cursor& c2 = Cursor::getInstance();

 

        cout << "&c1 = " << &c1 << endl;

        cout << "&c2 = " << &c2 << endl;

        return 0;

}

Posted by No names
,