Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:32:32+00:00 2026-05-23T02:32:32+00:00

I wanted to implement a concurrent object pool where in a shared_ptr is returned

  • 0

I wanted to implement a concurrent object pool where in a shared_ptr is returned and explicitly returning it to the pool is not required. I basically allocated an array pushed shared_ptrs for it in a concurrent queue and implemented a custom deletor. Seems to work. Am I missing anything?

#ifndef CONCURRENTOBJECTPOOL_H
#define CONCURRENTOBJECTPOOL_H

#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <tbb/concurrent_queue.h>

namespace COP
{

template<typename T>
class ConcurrentObjectPool;

namespace 
{
template<typename T>
class ConcurrentObjectPoolDeletor
{
public:
  ConcurrentObjectPoolDeletor(ConcurrentObjectPool<T>& aConcurrentObjectPool): 
  _concurrentObjectPool(aConcurrentObjectPool) {}

  void operator()(T *p) const;

private:
  ConcurrentObjectPool<T>& _concurrentObjectPool;  
};
} // Anonymous namespace for ConcurrentObjectPoolDeletor

template <typename T>
class ConcurrentObjectPool
{
 public:
 ConcurrentObjectPool(const unsigned int aPoolSize)
   : _goingDown(false),
     _poolSize(aPoolSize), 
     _pool(new T[_poolSize]),
     _ConcurrentObjectPoolDeletor(*this)
  {
    for(unsigned int i = 0; i < _poolSize; ++i)
      {
        boost::shared_ptr<T> curr(&_pool[i], _ConcurrentObjectPoolDeletor);
        _objectQueue.push(curr);
      }
  }

  boost::shared_ptr<T> loan()
  {
    boost::shared_ptr<T> curr;
    _objectQueue.pop(curr);
    return curr;
  }

  ~ConcurrentObjectPool()
  {
    _goingDown = true;
    _objectQueue.clear();
  }

 private:
  void payBack(T * p)
  {
    if (! _goingDown)
      {
        boost::shared_ptr<T> curr(p, _ConcurrentObjectPoolDeletor);
        _objectQueue.push(curr);
      }
  }

  bool _goingDown;
  const unsigned int _poolSize; 
  const boost::shared_array<T> _pool;
  const ConcurrentObjectPoolDeletor<T> _ConcurrentObjectPoolDeletor;
  tbb::concurrent_bounded_queue<boost::shared_ptr<T> > _objectQueue;
  friend class ConcurrentObjectPoolDeletor<T>;
};

namespace
{
template<typename T>
void ConcurrentObjectPoolDeletor<T>::operator()(T *p) const
{
  _concurrentObjectPool.payBack(p);
}
} // Anonymous namespace for ConcurrentObjectPoolDeletor

} // Namespace COP

#endif // CONCURRENTOBJECTPOOL_H
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:32:32+00:00Added an answer on May 23, 2026 at 2:32 am

    There is a race between setting the _goingDown flag in the destructor of ConcurrentObjectPool and reading the flag in payBack(). It can lead to memory leaks.

    Actually, maybe it’s better if you do not try to make the destructor safe to run concurrently with payBack(). It’s not safe anyway, starting from the fact that the _goingDown flag is a part of the pool object and so accessing it after the pool is destroyed would cause undefined behavior – i.e. all objects must be returned to the pool before it is destroyed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically i wanted to implement a popup UIView so i followed what was posted
I wanted to implement python com server using win32com extensions. Then consume the server
I wanted to implement the game Pacman. For the AI, I was thinking of
Let's say you wanted to implement a breadth-first search of a binary tree recursively
I wanted to implement web hooks in python. Both at server end and client
i just loved the fullcalendar and wanted to implement it in a small application,
I have an ImageView for which I wanted to implement the onClickListener. But when
Can I Implement Caching in MVC, If so how? I wanted to implement Cache
I am exploring iOS4.3 SDK & wanted to implement a particular animation effect. But
I wanted to implement an ajax based autocomplete feature for my searchbox, and i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.