Is there an open source C ++ object pool thread implementation?

I need to create a socket pool that will be served by multiple worker threads. Is there an implementation of a streaming pool of objects with functionality similar to Apache Commons' GenericObjectPool?

+5
source share
3 answers

Check out boost . flyweight .

+2
source

I usually use TBB to implement thread-safe scalable pools.

    template <typename T>
    class object_pool
    {
        std::shared_ptr<tbb::concurrent_bounded_queue<std::shared_ptr<T>>> pool_;
    public:
        object_pool() 
        : pool_(new tbb::concurrent_bounded_queue<std::shared_ptr<T>>()){}

        // Create overloads with different amount of templated parameters.
        std::shared_ptr<T> create() 
        {         
              std::shared_ptr<T> obj;
              if(!pool_->try_pop(obj))
                  obj = std::make_shared<T>();

              // Automatically collects obj.
              return std::shared_ptr<T>(obj.get(), [=](T*){pool_->push(obj);}); 
        }
    };
+4
source

, , Poco (Portable Components - ++-).

Poco::ObjectPool - . . , factory, , , .

, . Poco , . borrowObject() -, .

+1

All Articles