#ifndef THREAD_H
#define THREAD_H
#include <iostream>
#ifdef _WIN32
#include <Windows.h>
#endif
using namespace std;
template <class DataType>
class Thread
{
public :
Thread(DataType *thread_data = NULL){data = thread_data; running = false; }
#ifdef _WIN32
DWORD (*ThreadFunction)(LPVOID data);
friend DWORD _stdcall GetThreadFunction(LPVOID param)
{
Thread<DataType> *thread = (Thread<DataType>*)param;
return thread->ThreadFunction((void*)thread->data);
}
void Create()
{
running = true;
thread_handle = CreateThread(NULL,0,GetThreadFunction,this,0,&thread_id);
if(thread_handle == NULL)
{
cout<<"Failed to create thread!"<<endl;
return;
}
}
void Close()
{
CloseHandle(thread_handle);
running = false;
}
#endif
bool is_running(){ return running; }
protected :
#ifdef _WIN32
DWORD thread_id;
HANDLE thread_handle;
#endif
DataType *data;
bool running;
};
#endif