This is my last question. I try to improve my class Thread. Constructor receives pointer to the function, that must run in a new thread.
class Thread {
public:
Thread(void (*p)()) {
pf=p;
}
~Thread () {}
void StartThread() {
hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc, NULL, 0, &threadID);
}
private:
void (*pf)();
HANDLE hThread;
unsigned threadID;
static unsigned WINAPI ThreadProc(LPVOID lpParam) {
(*pf)(); //error C2597 illegal reference to non-static member
return 0;
}
};
In ThreadProc I need to call TimerFunc.
void TimerFunc () {
i++;
}
Example of usage this class:
Thread *timer;
timer = new Thread(TimerFunc);
timer->StartThread();
So it doesn't work. Please somebody tell me, if this class is foolish. May be it is a bad idea to send pointer to func which is situated outside class?? Thank you.
Thanks very much for your advices! Now it works!
class Thread {
public:
Thread(void (*p)()) {
gg.pf=p;
}
~Thread ();
void StartThread() {
hThread = (HANDLE)_beginthreadex( NULL, 0, ThreadProc, this, 0, &threadID);
}
private:
struct mm {
Thread *pThread;
void (*pf)();
} gg;
HANDLE hThread;
unsigned threadID;
static unsigned WINAPI ThreadProc(LPVOID lpParam) {
mm hh;
hh.pThread=static_cast<Thread*> (lpParam);
hh.pf=hh.pThread->gg.pf;
hh.pf();
return 0;
}
};
What do you think? Is it correct choice?