#include "VrThreadPool.h" VrThreadPool::VrThreadPool() { m_vetThread.clear(); m_deqRecoveryThread.clear(); } int VrThreadPool::ExecTask(ThreadExecFunc fExecFunc, void *pParam) { VrThread* pThread = _GetThread(); if(nullptr == pThread) return false; // 线程执行 return pThread->ExecTask(fExecFunc, pParam, std::bind(&VrThreadPool::_ExecFinish, this, std::placeholders::_1), pThread); } VrThread* VrThreadPool::_GetThread() { VrThread* pThread = nullptr; // 缓存里面 std::lock_guard lck(m_mutexPool); if(!m_deqRecoveryThread.empty()) { pThread = m_deqRecoveryThread.front(); m_deqRecoveryThread.pop_front(); return pThread; } // 缓存里面没有就创建新的吧 pThread = new VrThread(m_vetThread.size()); pThread->Init(); m_vetThread.push_back(pThread); return pThread; } void VrThreadPool::_ExecFinish(void *pObject) { VrThread* pThread = reinterpret_cast(pObject); if(pThread) { std::lock_guard lck(m_mutexPool); m_deqRecoveryThread.push_front(pThread); } }