Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Design for shared data (http://www.velocityreviews.com/forums/t754444-design-for-shared-data.html)

ittium 09-25-2011 06:47 AM

Design for shared data
 
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

Qi 09-25-2011 08:45 AM

Re: Design for shared data
 
On 2011-9-25 14:47, ittium wrote:
> 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/scottdensmor...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.


--
WQ

Marcel Müller 09-25-2011 01:14 PM

Re: Design for shared data
 
On 25.09.11 10.45, Qi wrote:
> On 2011-9-25 14:47, ittium wrote:
>> 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/scottdensmor...25/140827.aspx


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

ittium 09-25-2011 02:53 PM

Re: Design for shared data
 
On 25-09-2011 PM 06:44, Marcel Müller wrote:
> On 25.09.11 10.45, Qi wrote:
>> On 2011-9-25 14:47, ittium wrote:
>>> 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/scottdensmor...25/140827.aspx

>
> 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.


All times are GMT. The time now is 11:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.