I have a class that contains a function that calls create thread, and needs to pass itself (this) as a parameter:
DWORD threadId;
HANDLE h = CreateThread( NULL, 0, runThread, this, 0, &threadId);
My runThread definition is as follows:
DWORD WINAPI runThread(LPVOID args)
{
Obj *t = (Obj*)args;
t->funct();
return 0;
}
Unfortunately, the object t that I get in runThread() gets garbage. My Obj class has a function pointer attribute. Could that be the problem?
class Obj{
void(*funct)();
and in the constructor:
Obj(void(*f)())
{
funct = f;
}
where is my mistake? The function pointer, the createThread itself, or type-casting? I tried whatever I could think of.