[add] loading thread

This commit is contained in:
kimnuri
2025-11-19 23:02:13 +09:00
parent f50c483667
commit 402c6c1567
9 changed files with 91 additions and 87 deletions
+27 -31
View File
@@ -10,6 +10,19 @@ CThreadHolder::~CThreadHolder()
{
}
HRESULT CThreadHolder::Initialize(_uint iSize)
{
m_bStop_All = false;
m_iNumThreads = iSize;
m_Workers.reserve(iSize);
for (_uint i = 0; i < m_iNumThreads; ++i) {
m_Workers.emplace_back([this]() { this->WorkerThread(); });
}
return S_OK;
}
void CThreadHolder::WorkerThread()
{
while (true)
@@ -17,47 +30,19 @@ void CThreadHolder::WorkerThread()
function<void()> job = {};
unique_lock<mutex> lock(m_mtxJobQueue);
{
m_cvJobQueue.wait(lock, [this]() {
return !this->m_JobQueue.empty() || m_bStop_All;
});
m_cvJobQueue.wait(lock, [this]() { return !this->m_JobQueue.empty() || m_bStop_All; });
if (m_bStop_All && this->m_JobQueue.empty()) {
return;
}
function<void()> job = move(m_JobQueue.front());
job = move(m_JobQueue.front());
m_JobQueue.pop();
}
lock.unlock();
} lock.unlock();
job();
}
}
void CThreadHolder::EnqueueJob(function<void()> job)
{
if (m_bStop_All) {
throw runtime_error("ThreadPool 사용 중지됨");
}
lock_guard<mutex> lock(m_mtxJobQueue);
m_JobQueue.push(move(job));
m_cvJobQueue.notify_one();
}
HRESULT CThreadHolder::Initialize(_uint iSize)
{
m_bStop_All = false;
m_iNumThreads = iSize;
m_Workers.reserve(iSize);
for (_uint i = 0; i < m_iNumThreads; ++i) {
m_Workers.emplace_back([this]() {this->WorkerThread(); });
}
return S_OK;
}
CThreadHolder* CThreadHolder::Create(_uint iSize)
{
CThreadHolder* pInstance = new CThreadHolder();
@@ -70,6 +55,17 @@ CThreadHolder* CThreadHolder::Create(_uint iSize)
void CThreadHolder::Free()
{
__super::Free();
{
unique_lock<mutex> lock(m_mtxJobQueue);
m_bStop_All = true;
}
m_cvJobQueue.notify_all();
for (auto& thread : m_Workers) {
thread.join();
}
}
void CThreadHolder::Describe_Entity()