From dc914b18064b1a2f4f544774f81c2c449e5428ac Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 16 Jan 2017 15:40:01 -0500 Subject: [PATCH] multithreaded memory pool --- util.h | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/util.h b/util.h index 3b30919e..2ee1c286 100644 --- a/util.h +++ b/util.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #ifdef ANDROID @@ -71,11 +72,44 @@ namespace util std::bind (&MemoryPool::Release, this, std::placeholders::_1)); } - private: + protected: T * m_Head; }; + template + class MemoryPoolMt: public MemoryPool + { + public: + + MemoryPoolMt () {}; + template + T * AcquireMt (TArgs&&... args) + { + if (!this->m_Head) return new T(args...); + std::lock_guard l(m_Mutex); + return this->Acquire (args...); + } + + void ReleaseMt (T * t) + { + std::lock_guard l(m_Mutex); + this->Release (t); + } + + templateclass C, typename... R> + void ReleaseMt(const C& c) + { + std::lock_guard l(m_Mutex); + for (auto& it: c) + this->Release (it); + } + + private: + + std::mutex m_Mutex; + }; + namespace net { int GetMTU (const boost::asio::ip::address& localAddress);