CreateThread() arguement error
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.
# 1 Re: CreateThread() arguement error
The problem is that the function you are trying to start as a thread is a class member function. A thread function needs to have life outside the class instance, so it needs to either be a global function or a static member function.
Hope that helps.
krmed at 2007-11-11 4:12:15 >

# 2 Re: CreateThread() arguement error
You can only pass static member functions OR global funcitons as thread start routine, that too in specific format of signature.
Look into MSDN's description of CreateThread and ThreadProc.
Threading FAQ (http://dev-archive.earthweb.com/forum/showthread.php?p=1201804#sdk_thread)
# 3 Re: CreateThread() arguement error
Thanks for all the replies, in my actual implementation I do declare the member function as static, so actually my mistake which I just found out is THREADPROC need to be in this prototype
DWORD WINAPI THREADPROC(LPVOID)
which I did not follow. Thank you.