WhorlyWhelk
July 30th, 2007, 05:00 AM
I have been trying to make this.
Here is what I have come up with. Can you give ideas to make this better or point out problems? It is my first attempt and I've only tried it with a trivial program so far.
Thanks for any help!
workerthread.h:
#ifndef WORKERTHREAD_H
#define WORKERTHREAD_H
using namespace fastdelegate;
class WorkerThread {
private:
bool created,quit;
unsigned int workerID;
HANDLE h,hEvent,wMutex;
FastDelegate0<BOOL> funclist[6]; // ptrs for any BOOL fn()
int fd;
public:
WorkerThread(); // initialize
~WorkerThread(); // destructor
BOOL CreateWorker(); // begin thread
HANDLE GetHandle(){ return h; } // get thread handle
BOOL AddWork(BOOL (*func)()); // add function
void Quit(); // terminate thread
friend unsigned __stdcall threadEntry(void*);
};
#endif
workerthread.cpp:
#include <windows.h>
#include <process.h>
#include "FastDelegate.h"
#include "workerthread.h"
using namespace fastdelegate;
WorkerThread::WorkerThread(){
created=false;
quit=false;
fd=0;
h=NULL;
hEvent=CreateEvent(NULL,true,false,NULL);
wMutex=CreateMutex(NULL,false,NULL);
}
WorkerThread::~WorkerThread(){
WaitForSingleObject(h,INFINITE);
CloseHandle(h);
}
BOOL WorkerThread::CreateWorker(){
if (created)
return FALSE;
h=(HANDLE)_beginthreadex(NULL,0,&threadEntry,(void*)this,0,&workerID);
if (h==NULL)
return FALSE;
created=true;
return TRUE;
}
BOOL WorkerThread::AddWork(BOOL (*func)()){
WaitForSingleObject(wMutex,INFINITE);
if (fd>5){
ReleaseMutex(wMutex);
return FALSE;
}
funclist[fd].bind(func);
fd++;
ReleaseMutex(wMutex);
SetEvent(hEvent);
return TRUE;
}
void WorkerThread::Quit(){
WaitForSingleObject(wMutex,INFINITE);
quit=true;
ReleaseMutex(wMutex);
SetEvent(hEvent);
}
unsigned __stdcall threadEntry(void* arg){
WorkerThread* w=(WorkerThread*)arg;
while (1){
WaitForSingleObject(w->hEvent,INFINITE);
WaitForSingleObject(w->wMutex,INFINITE);
if (w->quit){
ReleaseMutex(w->wMutex);
break;
}
FastDelegate0<BOOL> func(w->funclist[w->fd-1]);
//w->funclist[w->fd-1]();
w->fd--;
if (w->fd==0)
ResetEvent(w->hEvent);
ReleaseMutex(w->wMutex);
(func)();
}
CloseHandle(w->hEvent);
}
Here is what I have come up with. Can you give ideas to make this better or point out problems? It is my first attempt and I've only tried it with a trivial program so far.
Thanks for any help!
workerthread.h:
#ifndef WORKERTHREAD_H
#define WORKERTHREAD_H
using namespace fastdelegate;
class WorkerThread {
private:
bool created,quit;
unsigned int workerID;
HANDLE h,hEvent,wMutex;
FastDelegate0<BOOL> funclist[6]; // ptrs for any BOOL fn()
int fd;
public:
WorkerThread(); // initialize
~WorkerThread(); // destructor
BOOL CreateWorker(); // begin thread
HANDLE GetHandle(){ return h; } // get thread handle
BOOL AddWork(BOOL (*func)()); // add function
void Quit(); // terminate thread
friend unsigned __stdcall threadEntry(void*);
};
#endif
workerthread.cpp:
#include <windows.h>
#include <process.h>
#include "FastDelegate.h"
#include "workerthread.h"
using namespace fastdelegate;
WorkerThread::WorkerThread(){
created=false;
quit=false;
fd=0;
h=NULL;
hEvent=CreateEvent(NULL,true,false,NULL);
wMutex=CreateMutex(NULL,false,NULL);
}
WorkerThread::~WorkerThread(){
WaitForSingleObject(h,INFINITE);
CloseHandle(h);
}
BOOL WorkerThread::CreateWorker(){
if (created)
return FALSE;
h=(HANDLE)_beginthreadex(NULL,0,&threadEntry,(void*)this,0,&workerID);
if (h==NULL)
return FALSE;
created=true;
return TRUE;
}
BOOL WorkerThread::AddWork(BOOL (*func)()){
WaitForSingleObject(wMutex,INFINITE);
if (fd>5){
ReleaseMutex(wMutex);
return FALSE;
}
funclist[fd].bind(func);
fd++;
ReleaseMutex(wMutex);
SetEvent(hEvent);
return TRUE;
}
void WorkerThread::Quit(){
WaitForSingleObject(wMutex,INFINITE);
quit=true;
ReleaseMutex(wMutex);
SetEvent(hEvent);
}
unsigned __stdcall threadEntry(void* arg){
WorkerThread* w=(WorkerThread*)arg;
while (1){
WaitForSingleObject(w->hEvent,INFINITE);
WaitForSingleObject(w->wMutex,INFINITE);
if (w->quit){
ReleaseMutex(w->wMutex);
break;
}
FastDelegate0<BOOL> func(w->funclist[w->fd-1]);
//w->funclist[w->fd-1]();
w->fd--;
if (w->fd==0)
ResetEvent(w->hEvent);
ReleaseMutex(w->wMutex);
(func)();
}
CloseHandle(w->hEvent);
}