Click to See Complete Forum and Search --> : CreateThread() arguement error


keenlearner
July 17th, 2007, 07:51 AM
Hallo, when I use class and I put one of the member function as the CreateThread argument, I keep getting the compile error in Visual Studio 2005.


#include <windows.h>

class X {
public:
void f();
void s(){};
};

void X::f()
{
//error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (__thiscall X::* )(void)' to 'LPTHREAD_START_ROUTINE'
HANDLE hThread = CreateThread(NULL, 0, &X::s, (void *) 1, 0, NULL);

//error C3867: 'X::s': function call missing argument list; use '&X::s' to create a pointer to member
HANDLE hThread2 = CreateThread(NULL, 0, s, (void *) 1, 0, NULL);
}
int main() {
return 0;
}


What should I put for the third argument of CreateThread ? Thank you.

MikeAThon
July 17th, 2007, 11:02 AM
Declare the member function as a "static" member function, as explained in this FAQ: "How to use member functions as thread functions?" at http://www.codeguru.com/forum/showthread.php?t=312453

Mike

PS: This is a common question. It would have been better if you searched a bit harder before asking it, since the answer is readily available.

keenlearner
July 17th, 2007, 06:23 PM
Thanks, my actual implementation has the static member function declared, my actual mistakes is I do not follow the prototype.
DWORD WINAPI threadProc(LPVOID )
I have figure my problem for few hours and failed up so I post it to get help, but only found the protype is my mistake. Thanks.

MrViggy
July 17th, 2007, 06:50 PM
Thanks, my actual implementation has the static member function declared, my actual mistakes is I do not follow the prototype.
DWORD WINAPI threadProc(LPVOID )
I have figure my problem for few hours and failed up so I post it to get help, but only found the protype is my mistake. Thanks.
Well, if you don't post your actual code, we cannot correctly fix your actual problem. The code and question you posted was correctly answered by Mike.

Viggy