5 Replies - 1005 Views - Last Post: 09 September 2011 - 09:58 AM Rate Topic: -----

#1 Roig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-August 08

Order of initialization in Singletons.

Posted 09 September 2011 - 05:45 AM

Hello!

I'm programming an application that i want to startup and shutdown subsystems. But don't know how can I do it exactly without static initialization order fiasco.

Right now I'm using this for creating my singletons: Example

class B {
public:
	static B * GetInstance(void);
	~B();

private:
	B();
	static B * s_B;
};


B * B::s_B = 0;

B::B()
{
	// DO NOTHING
}

B::~B()
{
	delete s_B;
}

B * B::GetInstance()
{
	if( s_B == 0 )
	{
		s_B=new B();
	}
	return s_B;
}



If I code my subsystems like above, can I achieve the correct startup and shutdown without static initialization order problems?


I'm reading a book that suggests this way:

class A {
public:
	A()
	{  
		//DO NOTHING
	};
	~A()
	{
		//DO NOTHING
	};
	void startUp()
	{
		//START SUBSYTEM
	};
	void shutDown()
	{
		//SHUTDOWN SUBSYSTEM
	};
};



Then we can do:

A g_A();
C g_C()
int main ()
{
 g_A.startUp();
 g_C.startUp();

 // SIMULATION CODE

 g_C.shutDown();
 g_A.shutDown();

return 0;
}




Which one should be better? Thank you and sorry for my English :)

Is This A Good Question/Topic? 0
  • +

Replies To: Order of initialization in Singletons.

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2130
  • View blog
  • Posts: 4,196
  • Joined: 30-May 10

Re: Order of initialization in Singletons.

Posted 09 September 2011 - 06:29 AM

Does this help?
http://www.parashift....html#faq-10.15
Was This Post Helpful? 2
  • +
  • -

#3 Coding in the name of  Icon User is offline

  • D.I.C Regular

Reputation: 67
  • View blog
  • Posts: 291
  • Joined: 07-July 10

Re: Order of initialization in Singletons.

Posted 09 September 2011 - 06:39 AM

You might want to take a look at this, either for design inspiration if you decide to roll your own, or just to use.
Was This Post Helpful? 1
  • +
  • -

#4 Roig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-August 08

Re: Order of initialization in Singletons.

Posted 09 September 2011 - 06:51 AM

View PostSalem_c, on 09 September 2011 - 07:29 AM, said:



Yes, It helped me to understand the problem. But I don't fully understand the solution that proposes.. Isn't it more easy to do the second approach in my first post? With startUp and shutDown?

P.D. I don't want to use boost yet. I think that I need to learn more C++ before I go with boost.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,635
  • Joined: 16-October 07

Re: Order of initialization in Singletons.

Posted 09 September 2011 - 08:14 AM

I prefer my singletons like this:
#include <iostream>

class B {
public:
	static B &GetInstance();
	int n;
private:
	B();
};

using namespace std;

B::B() : n(42) { std::cout << "Instance B!" << std::endl; }

B &B::GetInstance() {
	static B instance;
	return instance;
}

void test() {
	cout << "n = " << B::GetInstance().n << endl;
	B::GetInstance().n++;
}

int main() {
	test();
	test();
	test();
}



Pointers are messy. ;)

If order of instantiation is important, then I wouldn't let all the related classes be singletons at all. Create a container for them and make that a singleton.

#include <iostream>

class AB;

class A {
public:
	int n;
private:
	A(); friend class AB;
};

class B {
public:
	int n;
private:
	B(); friend class AB;
};

class AB {
private:
	A *a;
	B *b;
	AB();
public:
	static AB &Instance();
	A &getA() { return *a; }
	B &getB() { return *b; }
	~AB();
};

using namespace std;

A::A() : n(69) { std::cout << "Instance A!" << std::endl; }
B::B() : n(42) { std::cout << "Instance B!" << std::endl; }
AB::AB() { 
	std::cout << "Instance AB!" << std::endl;
	a = new A();
	b = new B();
}
AB::~AB() { delete a; delete b; }

AB &AB::Instance() {
	static AB instance;
	return instance;
}

void test() {
	cout << "A.n = " << AB::Instance().getA().n++ << endl;
	cout << "B.n = " << AB::Instance().getB().n-- << endl;
}

int main() {
	test();
	test();
	test();
}


Was This Post Helpful? 1
  • +
  • -

#6 Roig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 17-August 08

Re: Order of initialization in Singletons.

Posted 09 September 2011 - 09:58 AM

Wow nice info baavgai Thanks :)

Using your method I got something but I'm not sure if what I did is correct :D.
Take a look, I have a ContainerOfSubsystems class singleton which will contain 2 subsystems singletons C and D.

The idea is that the order of start/shutdown subsystems will be inside the method startUpSubsystems/shutDownSubsystems inside ContainerOfSubsystems object.

Now the code:

#include <iostream>
using namespace std;
class C {
	public:
		C(){}; //NOTHING
		~C(){};//NOTHING
		void	startUp()	{
			cout << "C START"<< endl;
		};
		void	shutDown()	{
			//SHUTDOWN CODE
			cout << "C DOWN"<< endl;
		};
		static	C &GetInstance()	{
			static  C Instance;
			return Instance;
		};
};

class D {
	public:

		D(){}; //NOTHING
		~D(){};//NOTHING
		void	startUp() {
			//STARTUP CODE
			cout << "D START"<< endl;
		};
		void	shutDown()	{
			//SHUTDOWN CODE
			cout << "D DOWN"<< endl;
		};
		static	D &GetInstance()	{
			static  D Instance;
			return Instance;
		};
};

class ContainerOfSubsystems {
	public:
		ContainerOfSubsystems(){}; //NOTHING
		~ContainerOfSubsystems(){};//NOTHING
		void	startUpSubsystems()		{
			g_C.GetInstance().startUp();
			g_D.GetInstance().startUp();

		};
		void	shutDownSubsystems()	{
			g_D.GetInstance().shutDown();
			g_C.GetInstance().shutDown();
		};
		static	ContainerOfSubsystems &GetInstance()	{
			static  ContainerOfSubsystems Instance;
			return Instance;
		};
	private:
		C g_C;
		D g_D;

};


int main() {

ContainerOfSubsystems Test;

Test.GetInstance().startUpSubsystems();
Test.GetInstance().shutDownSubsystems();

return 0;
}



Well, I think this way I can achieve the correct order specified inside ContainerOfSubsystems, isn't it?

P.D. How can I destroy the static objects when shutDown for every subsystem is called? .. I think I can't O_o >_<

Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1