Skip to content

Instantly share code, notes, and snippets.

@RklAlx
Created September 27, 2013 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RklAlx/6727661 to your computer and use it in GitHub Desktop.
Save RklAlx/6727661 to your computer and use it in GitHub Desktop.
Threads
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;
}
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