#include "Thread.h"
struct ThreadParam
{
THREAD_PROC threadProc;
void* param;
};
#ifdef OS_WIN
static unsigned int _stdcall ThreadEntry(void* param)
{
ThreadParam* p = (ThreadParam*)param;
p->threadProc(p->param);
delete p;
return 0;
}
int Thread::Start(THREAD_PROC threadProc, void* param)
{
ThreadParam* p = new ThreadParam;
p->threadProc = threadProc;
p->param = param;
m_thread = (HANDLE)::_beginthreadex(NULL, 0, ThreadEntry, p, 0, 0);
return m_thread != NULL ? 0 : -1;
}
int Thread::Join()
{
WaitForSingleObject(m_thread, INFINITE);
CloseHandle(m_thread);
return 0;
}
void Thread::Sleep(unsigned milli)
{
::Sleep(milli);
}
#else
static void* ThreadEntry(void* param)
{
ThreadParam* p = (ThreadParam*)param;
p->threadProc(p->param);
delete p;
return 0;
}
int Thread::Start(THREAD_PROC threadProc, void* param)
{
ThreadParam* p = new ThreadParam;
p->threadProc = threadProc;
p->param = param;
pthread_create(&m_thread, NULL, ThreadEntry, p);
return 0;
}
int Thread::Join()
{
pthread_join(m_thread, NULL);
pthread_detach(m_thread);
return 0;
}
void Thread::Sleep(unsigned milli)
{
usleep(milli*1000);
}
#endif