C++ 对象池模版

#ifndef OBJ_POOL_H
#define OBJ_POOL_H
#include <list>
#include <unordered_set>
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
 
template <typename T, typename ObjBuilder>
class ObjPool {
public:
    ObjPool(int initSize, int maxSize) {
        _initSize = initSize;
        _maxSize = maxSize;
        for (int i = 0; i < _initSize; ++i) {
            T* t = build();
            _objQueue.push_back(t);
        }
    }
    ~ObjPool() {
        for (auto i : _objQueue) {
            delete i;
        }
        _objQueue.clear();
        for (auto i : _activeSet) {
            delete (i);
        }
        _activeSet.clear();
    }

    T* get() {

        bool objQueueIsEmpty = false;
        {
            std::lock_guard<std::mutex> lock_guard(_lock);
            size_t objQueueIsEmpty = _objQueue.empty();
            size_t activeSetSize = _activeSet.size();
            if (objQueueIsEmpty && activeSetSize < _maxSize) {
                T *t = build();
                _activeSet.insert(t);
                return t;
            }
        }
       
        while (1) {
            {
                std::lock_guard<std::mutex> lock_guard(_lock);
                objQueueIsEmpty = _objQueue.empty();
                if (!objQueueIsEmpty) {
                    T* t = _objQueue.front();
                    _objQueue.pop_front();
                    _activeSet.insert(t);
                    return t;
                }

            }
            //没获取到等待其他线程释放锁
            std::unique_lock<std::mutex> lock(_waitMutex); 
            _waitConditionVariable.wait(lock);
        }
        assert(false);
    }

    void release(T* t) {
        std::lock_guard<std::mutex> lock_guard(_lock);
        if (_activeSet.find(t) != _activeSet.end()) {
            _activeSet.erase(t);
        }
        _objQueue.push_back(t);
        _waitConditionVariable.notify_one();
    }

    inline T* build() {
        return ObjBuilder::build();
    }

private:
    std::list<T*> _objQueue;
    std::unordered_set<T*> _activeSet;
    int _initSize;
    int _maxSize;
    std::mutex _waitMutex;
    std::condition_variable _waitConditionVariable;
    std::mutex _lock;
};
//如果_instance 已经被销毁,这个怎么办
template <typename Pool, typename T>
class ObjPoolGuard {
public:
    ObjPoolGuard(Pool* pool, T* t) {
        _pool = pool;
        _instance = t;
    }
    ~ObjPoolGuard() {
        if (_instance != nullptr) {
            _pool->release(_instance);
        }
    }
private:
    Pool* _pool;
    T* _instance;
};

#ifdef TEST_OBJPOOL

class CTestObj{
public:
    CTestObj(){
        printf("new CTestObj \n");
    }
    void DoSomething(int i){
        printf("new hahahahah \n");
    }
};

class CTestBuild{
public:
    static CTestObj* build(){
        return new CTestObj();
    }
};
typedef  ObjPool<CTestObj,CTestBuild> TestPool;

void Process(void* v){
    TestPool* pool = (TestPool*)v ;
    CTestObj* obj = pool->get();
    ObjPoolGuard<TestPool,CTestObj> aa(pool,obj);
    obj->DoSomething(11);
    sleep(1);
}


int main(int args ,char **argv){
    TestPool* pool= new TestPool(2,5);
    std::thread threads[10];
    for (int i = 0; i < 10; ++i){
        threads[i] = std::thread(Process, (void *)pool);
    }

    for (auto & th:threads)
        th.join();
    
    return 0;
};


#endif

#endif //OBJ_POOL_H

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值