C++ Telltales part 7:

The Object Pool Pattern

Heikki Hellgren
Oct 4, 2018 · 2 min read

This is seventh part of my C++ Telltales series where I share some tips and tricks to work with C++. Feel free to check out also other parts of the series here!

Object pools are useful if you need to have large number of objects which have very short lifetime and especially if the cost of initialization or destruction of those objects is high. They are used to provide access to reusable objects which are either created on the fly when requested or created upon object pool initialization — which way to use this depends on how you are planning to use the object pool.

Object pools can offer great performance boost in some cases and are very often used for example in graphics particle systems, storing large bitmaps and fonts and for socket connections. Here is an example implementation of an object pool utilizing C++14. It allows initializing the pool with some number of objects, setting the maximum number of objects in the pool and also passing construction parameters for the newly created objects by utilizing C++ parameter pack. So let’s have a look at it:

It’s most likely not the perfect solution but provides ideas how to do object pools. The example uses std::unique_ptr custom deleter to return the object back to the pool once the calling context has finished working with it. It requires that the object is reseted to it’s original state using the reset functionality so that when the same object instance is used again, it will have all member variables set as they would be for a totally new instance.

Please note also that the example is not thread-safe at all so calling it from multiple threads will not work. You can easily make it thread-safe by just adding mutexes to places where the stack is being modified. You might want to make the object pool a singleton or mapleton to access it from multiple contexts easily.

Feel free to use this snippet for basis of your own object pool implementation, happy to help!

Image for post

If you liked the story, please press the ❤ button below (did you know that you can give more than one clap). Also please feel free to share this story!

About me

I am Heikki Hellgren, Software Expert and technology enthusiast working at Elektrobit Automotive. My interests are in software construction, tools, automatic testing and all the new and cool stuff like AI and autonomous driving. You can follow me on Medium and Twitter and check out my websitefor more information.

Sign up for Get Better Tech Emails via HackerNoon.com

By HackerNoon.com

how hackers start their afternoons. the real shit is on hackernoon.com. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Heikki Hellgren

Written by

Father of two, husband and Lead Developer @ OP Financial. https://drodil.kapsi.fi

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Heikki Hellgren

Written by

Father of two, husband and Lead Developer @ OP Financial. https://drodil.kapsi.fi

HackerNoon.com

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store