Design for shared data

I

ittium

Group,
I have a design a multi-threaded system that uses good amount of shared
data. This data is needed in multiple classes/functions. I have three
designs for it.
1. Keep the data global, so that it can be accessed any where
2. Wrap the data in singleton class and use it wherever needed by
getInstance static method.
3. Pass the data by constant reference where ever needed. In this case
if function is part of some function hierarchy then data will have to be
passed from topmost function to the function where it is used.

All these approaches will solve the issue. I am not able to see
advantages or disadvantages of these approaches. Any new approach is
also welcome.

Please consider the solution form the point of view of synchronization
effort, since shared data is to be used in multi-threaded solution.
thanks
ittium
 
Q

Qi

Group,
I have a design a multi-threaded system that uses good amount of shared
data. This data is needed in multiple classes/functions. I have three
designs for it.
1. Keep the data global, so that it can be accessed any where
2. Wrap the data in singleton class and use it wherever needed by
getInstance static method.
3. Pass the data by constant reference where ever needed. In this case
if function is part of some function hierarchy then data will have to be
passed from topmost function to the function where it is used.


Singletons are evil
http://blogs.msdn.com/b/scottdensmore/archive/2004/05/25/140827.aspx

And they are more evil in multiple thread.

So your 1 and 2 approached are kicked out.

And 3 is the good approach, IMO.
 
M

Marcel Müller


Well, the alternative, dependency injection, becomes evil to, if it has
to be applied multiple times to the same classes for a dozen different
global services. It even might blow up the memory footprint of your
application it DI has to be applied to many objects, unless you pass the
reference to the global singleton as template parameter. So it depends.
And they are more evil in multiple thread.

This makes no difference at all. You have to take care of thread safety
anyway.


Marcel
 
I

ittium

Well, the alternative, dependency injection, becomes evil to, if it has
to be applied multiple times to the same classes for a dozen different
global services. It even might blow up the memory footprint of your
application it DI has to be applied to many objects, unless you pass the
reference to the global singleton as template parameter. So it depends.
Assuming that dependencies are mutable and so are always passed by
reference, dependency injection will blow up the memory footprint, if
you assume that most of the class will have large number of dependencies.

From the discussion here and some googling, it appears DI with factory
pattern seems to me as a good idea for average cases. I favor factory
pattern so that dependency instance searching can be centralized.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top