semaphore instead of busy waiting in CJobPool

This commit is contained in:
def 2017-09-02 11:32:10 +02:00
parent ed59b0f4a2
commit e64e59a839
3 changed files with 7 additions and 4 deletions

View file

@ -9,6 +9,7 @@ CJobPool::CJobPool()
m_NumThreads = 0;
m_Shutdown = false;
m_Lock = lock_create();
sphore_init(&m_Semaphore);
m_pFirstJob = 0;
m_pLastJob = 0;
}
@ -16,9 +17,12 @@ CJobPool::CJobPool()
CJobPool::~CJobPool()
{
m_Shutdown = true;
for(int i = 0; i < m_NumThreads; i++)
sphore_signal(&m_Semaphore);
for(int i = 0; i < m_NumThreads; i++)
thread_wait(m_apThreads[i]);
lock_destroy(m_Lock);
sphore_destroy(&m_Semaphore);
}
void CJobPool::WorkerThread(void *pUser)
@ -30,6 +34,7 @@ void CJobPool::WorkerThread(void *pUser)
CJob *pJob = 0;
// fetch job from queue
sphore_wait(&pPool->m_Semaphore);
lock_wait(pPool->m_Lock);
if(pPool->m_pFirstJob)
{
@ -49,8 +54,6 @@ void CJobPool::WorkerThread(void *pUser)
pJob->m_Result = pJob->m_pfnFunc(pJob->m_pFuncData);
pJob->m_Status = CJob::STATE_DONE;
}
else
thread_sleep(10);
}
}
@ -81,5 +84,6 @@ int CJobPool::Add(CJob *pJob, JOBFUNC pfnFunc, void *pData)
m_pFirstJob = pJob;
lock_unlock(m_Lock);
sphore_signal(&m_Semaphore);
return 0;
}

View file

@ -47,6 +47,7 @@ class CJobPool
volatile bool m_Shutdown;
LOCK m_Lock;
SEMAPHORE m_Semaphore;
CJob *m_pFirstJob;
CJob *m_pLastJob;

View file

@ -1,5 +1,4 @@
#include <base/system.h>
#include <iostream>
#include <engine/shared/config.h>
#include <engine/map.h>
@ -48,7 +47,6 @@ void CBackground::LoadBackground()
if(time_get()-m_LastLoad < 10*time_freq())
return;
std::cout << "here" << std::endl;
if(m_Loaded && m_pMap == m_pBackgroundMap)
m_pMap->Unload();