Using unique_ptr instead of raw pointers in object pool
This commit is contained in:
parent
b3b2366468
commit
799f334fa7
|
@ -9,7 +9,7 @@ class ObjectPool {
|
||||||
public:
|
public:
|
||||||
using ptr_type = std::unique_ptr<T, std::function<void(T*)>>;
|
using ptr_type = std::unique_ptr<T, std::function<void(T*)>>;
|
||||||
|
|
||||||
ObjectPool(std::function<T*()> createObject) :
|
ObjectPool(std::function<std::unique_ptr<T>()> createObject) :
|
||||||
createObject(createObject)
|
createObject(createObject)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public:
|
||||||
std::lock_guard<std::mutex> lock(poolMutex);
|
std::lock_guard<std::mutex> lock(poolMutex);
|
||||||
|
|
||||||
if (pool.empty()) {
|
if (pool.empty()) {
|
||||||
pool.push(std::unique_ptr<T>(createObject()));
|
pool.push(createObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr_type tmp(pool.top().release(), [this](T* p) {
|
ptr_type tmp(pool.top().release(), [this](T* p) {
|
||||||
|
|
Loading…
Reference in New Issue