Created
September 27, 2013 12:09
-
-
Save RklAlx/6727661 to your computer and use it in GitHub Desktop.
Threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool CMyObj::StartThread() | |
{ | |
m_Running = true; | |
m_hThreadHandle = CreateThread(NULL,0,ThreadFunction,this,0,&this->m_ThreadID); | |
if(m_hThreadHandle == nullptr) | |
m_Running = false; | |
return m_Running | |
} | |
void CMyObj::StopThread() | |
{ | |
m_Running = false; | |
WaitForSingleObject(m_hThreadHandle, INFINITE) | |
//for multiple threads, create an array of handle and use WaitForMultipleObjects(name_of_array, INIFINITE) | |
CloseHandle(m_hThreadHandle); | |
m_hThreadHandle = nullptr; | |
} | |
DWORD WINAPI CMyObj::ThreadFunction(LPVOID arg) | |
{ | |
CMyObject* _this = (CMyObject *) arg; | |
while(_this->m_Running) | |
{ | |
//do stuff | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CMyObj | |
{ | |
public: | |
CMyObj(); | |
~CMyObj(); | |
bool StartThread(); | |
void StopThread(); | |
private: | |
HANDLE m_hThreadHandle; | |
DWORD m_ThreadID; | |
bool m_Running; | |
static DWORD WINAPI ThreadFunction(LPVOID arg); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment