#ifndef _BT_THREAD_H
#define _BT_THREAD_H
#include <windows.h>
class btworkThread{
public:
btworkThread( void(*entry)(btworkThread* ,void*),void* user){
_entry = entry;
_param = user;
_thandle = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)btworkThread::run,user,NULL,&_tid);
}
void stop(){
_loop = false;
}
void wait(){
while(!_end){
Sleep(20);
}
Sleep(20); // ensure that thread has terminated
}
void stopAndWait(){
stop();wait();
}
bool loop(){
return _loop;
}
void code_begin(){
_loop = true; _begin = true;_end=false;
}
void code_end(){
_loop = false; _begin = false;_end=true;
CloseHandle(_thandle);
}
protected:
static void run(btworkThread* thread){
thread->_entry(thread,thread->_param);
}
private:
volatile bool _loop,_begin,_end;
void (*_entry)(btworkThread* ,void*);
void* _param;
DWORD _tid;
HANDLE _thandle;
};