Object pool pattern: Difference between revisions

Content deleted Content added
Grammatical error in summary
m Subst cite isbn using AWB
Line 34:
 
== Implementation ==
Object pools can be implemented in an automated fashion in languages like C++ via smart pointers. In the constructor of the smart pointer - an object can be requested from the pool and in the destructor of the smart pointer - the object can be released back to the pool. In garbage collected languages, where there are no destructors (which are guaranteed to be called as part of a stack unwind) - object pools MUST be implemented in a manual fashion, by explicitly requesting an object from the [[Factory (object-oriented programming)|factory]] and returning the object by calling a dispose method (as in the [[dispose pattern]]). Using a [[finalizer]] to do this is not a good idea as there are usually no guarantees on when (or if ever) the finalizer will be run. Instead - prefer using try ... finally to ensure that getting and releasing the object is exception neutral.
 
Manual object pools are simple to implement, but harder to use, as they require [[manual memory management]] of pool objects.
Line 72:
 
== Examples ==
 
=== C# ===
In the .NET [[Base Class Library]] there are a few objects that implement this pattern. <code>System.Threading.ThreadPool</code> is configured to have a predefined number of threads to allocate. When the threads are returned, they are available for another computation. Thus, one can use threads without paying the cost of creation and disposal of threads.
Line 168 ⟶ 169:
| url = http://www.kircher-schwanninger.de/michael/publications/Pooling.pdf
| accessdate = 2007-06-09 }}
* {{Cite book | isbn = 978-1-4302-4458-5 | title = Pro .NET Performance: Optimize Your C# Applications | last1 = Goldshtein | first1 = Sasha | last2 = Zurbalev | first2 = Dima | last3 = Flatow | first3 = Ido | year = 2012 | publisher = Apress | url = http://www.apress.com/9781430244585 | page = <!-- can't have both --> | pages = <!-- --> }}
* {{cite isbn|9781430244592}}
{{refend}}