GrabBag/Module/ThreadPool/Src/VrThreadPool.cpp

48 lines
1.2 KiB
C++
Raw Normal View History

2025-07-23 01:35:14 +08:00
#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<std::mutex> 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<VrThread*>(pObject);
if(pThread)
{
std::lock_guard<std::mutex> lck(m_mutexPool);
m_deqRecoveryThread.push_front(pThread);
}
}