Hello
I am writing a class that spawns and communicates with a worker thread. The class is going to be Polymorphic such that derived classes can implement their own work functions. This way I can have the thread-safe communication interface (mutex and STL containers) in the WorkerThread class.
class WorkerThread
{
public:
WorkerThread(void);
~WorkerThread(void);
private:
HANDLE hThread;
static DWORD WINAPI ThreadRoutine(LPVOID param);
virtual void ThreadFunction(LPVOID param) = 0; //implemented in derived classes
};
DWORD WINAPI WorkerThread::ThreadRoutine(LPVOID param)
{
ThreadFunction(param); //error C2352: 'WorkerThread::ThreadFunction' : illegal call of non-static member function
return 0;
}
Is this bad? Should I instead pass an object which has its own work function (derived from a base class)? But then I would have to pass some interface to the object.
DWORD WINAPI WorkerThread::ThreadRoutine(LPVOID param)
{
(somecastings)param->ThreadFunction(/*InterfaceToComm*/);
return 0;
}