#include "VrThread.h" VrThread::VrThread(int index) : m_nIndex(index) , m_bRuning(false) { } int VrThread::Init() { LOG_DEBUG("create thread : %d\n", m_nIndex); std::thread execThread(&VrThread::_ExecTask, this); execThread.detach(); while(!m_bRuning) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } return 0; } int VrThread::ExecTask(ThreadExecFunc fExecFunc, void *pParam, ExecFinishFunc pExecFinishFunc, void *pFinishParam) { if(nullptr == fExecFunc) return -1; m_fExecFunc = fExecFunc; m_pParam = pParam; m_fFinishFunc = pExecFinishFunc; m_pFinishParam = pFinishParam; m_condExec.notify_all(); return 0; } void VrThread::_ExecTask() { while(true) { LOG_DEBUG("thread: %d wait \n", m_nIndex); m_bRuning = true; std::unique_lock olck(m_mutexExec); m_condExec.wait(olck); LOG_DEBUG("thread: %d exec \n", m_nIndex); if(nullptr == m_fExecFunc) { m_nRet = -1; continue; } m_fExecFunc(m_pParam); //执行完 m_fExecFunc = nullptr; m_pParam = nullptr; if(m_fFinishFunc) { m_fFinishFunc(m_pFinishParam); } } }