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

  • SEARCH
  • Home
  • 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 6819509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:18:54+00:00 2026-05-26T21:18:54+00:00

Sorry that the question of this problem might be a bit vague. I’m trying

  • 0

Sorry that the question of this problem might be a bit vague. I’m trying to port this ObjectPool code from C# into C++ but seems there are some parts where I don’t know how I should proceed. Codes are as follows:

using System;

namespace FastRank
{
    public class ObjectPool<T> where T : class, new()
    {
        private int _count;

        private T[] _pool;

        public ObjectPool(int initSize)
        {
            _pool = new T[initSize];
        }

        public T Get()
        {
            lock (_pool)
            {
                if (_count > 0)
                {
                    --_count;
                    T item = _pool[_count];
                    _pool[_count] = null;
                    return item;
                }
            }
            return new T();
        }

        public void Return(T item)
        {
            lock (_pool)
            {
                if (_count == _pool.Length)
                    Array.Resize(ref _pool, _pool.Length*2 + 1);
                _pool[_count++] = item;
            }
        }
    }
}

My questions are:

1) How should I implement that constraint on generic parameter T in C++? (class, new())

2) Is there a simple way to implement the mutex lock part?

3) Will it be more efficient to define _pool as vector instead of T[] in C++?

edit -> Implemented something as:

#include "object_pool.h"

#include <boost/thread.hpp>
#include <vector>
using namespace std;

template <class T>
ObjectPool<T>::ObjectPool(int init_size) {
  pool_.reserve(init_size);
}

template <class T>
T ObjectPool<T>::Get() {
  boost::lock_guard<boost::mutex> lock(guard_);
  int sz = (int) pool_.size();
  if (sz == 0) {
    throw "Object pool size is now zero.";
  }
  else {
    T item = pool_[sz-1];
    pool_.pop_back();
    return item;
  } 
}

template <class T>
void ObjectPool<T>::Return(T item) {
  boost::lock_guard<boost::mutex> lock(guard_);
  pool_.push_back(item);
}

Wondering if there’s any problem with this code…

  • 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-26T21:18:54+00:00Added an answer on May 26, 2026 at 9:18 pm

    Here’s a naive snippet that illustrates one possible approach:

    #include <mutex>
    
    template <typename T>
    class SyncStack
    {
       T         * m_data;
       std::size_t m_size;
       std::size_t m_count;
       std::mutex  m_lock;
    
    public:
      T get()
      {
        std::lock_guard<std::mutex> lock(m_lock);
    
        if (m_count == 0) { throw UnderrunException; }
    
        --m_count;
        T x(m_data[m_count]);
        m_data[m_count].~T();
    
        return x;
      }
    
      void put(T x)
      {
        std::lock_guard<std::mutex> lock(m_lock);
        ::new (m_data + m_count) T(std::move(x));
        ++m_count;
      }
    };
    

    This example assumes that m_data points to infinite memory. Reallocation is a bit tricky and involves making lots of copies.

    A simpler approach would be to wrap your synchronized structure around another, existing standard container such as std::vector<T>.

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

Sidebar

Related Questions

Sorry, I realized that I put in all of my code in this question.
This question will be a little longer and I am sorry for that :)
Sorry if this is a dumb question but im creating a filter class that
Sorry if this is a stupid question... I've developed an application that creates absolute
I might be asking a very basic question and I am sorry for that.
This might be a dumb question. Sorry if it is. But Im working on
First of all, I'm sorry about the feedback-nature of this question. I'm trying to
Hello yeah I'm asking this question a second time, sorry about that but I
Sorry, I'm new to Java, so this question might be unclear. I have been
I understand that this question may get closed for being duplicate (sorry if it

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.