Why I can't beging thread from myclass constructor?
-
CMyClass::CMyClass() { m_thread=(HANDLE)_beginthreadex(0,0,&MyClass::MyFunct,0,0,0);//error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall CMyClass::* )(void *)' to 'unsigned int (__stdcall *)(void *)' } CMyClass::~CMyClass(void) { CloseHandle(m_thread); } unsigned __stdcall CMyClass::MyFunct(void* param) { return 0; }
-
CMyClass::CMyClass() { m_thread=(HANDLE)_beginthreadex(0,0,&MyClass::MyFunct,0,0,0);//error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall CMyClass::* )(void *)' to 'unsigned int (__stdcall *)(void *)' } CMyClass::~CMyClass(void) { CloseHandle(m_thread); } unsigned __stdcall CMyClass::MyFunct(void* param) { return 0; }
It has nothing to do with the fact that you start it in your constructor. The problem is that the 3rd argument you pass to beginthreadex is invalid: the function expects to have a pointer to a global function and you pass a pointer to a member function. The difference is that they don't have the same prototype: the member function has an implicit parameter: the this parameter (that allows the function to know to which instance of the class it belongs to). To solve the problem, you can declare the function as static (in that case, the this parameter is not passed). But of course, you won't be able to access members of the class which are non-static (because the function is global to all instances and thus doesn't belong to a specific instance). Another solution is to pass the pointer to a global function and pass the pointer to the instance of your class as the parameter (this). In the function, you'll then need to cast it to your class:
unsigned __stdcall MyGlobalFunct(void* param)
{
CMyClass* pClass = (CMyClass*)param;
pClass->MyMemFunction();
return 0;}
Cédric Moonen Software developer
Charting control [Updated - v1.1] -
CMyClass::CMyClass() { m_thread=(HANDLE)_beginthreadex(0,0,&MyClass::MyFunct,0,0,0);//error C2664: '_beginthreadex' : cannot convert parameter 3 from 'unsigned int (__stdcall CMyClass::* )(void *)' to 'unsigned int (__stdcall *)(void *)' } CMyClass::~CMyClass(void) { CloseHandle(m_thread); } unsigned __stdcall CMyClass::MyFunct(void* param) { return 0; }
What happens if you run this code
unsigned __stdcall MyFunct(void* param); ... ... m_thread=(HANDLE)_beginthreadex(0,0,&MyFunct,0,0,0); unsigned __stdcall MyFunct(void* param) { return 0; }
WhiteSky
-
It has nothing to do with the fact that you start it in your constructor. The problem is that the 3rd argument you pass to beginthreadex is invalid: the function expects to have a pointer to a global function and you pass a pointer to a member function. The difference is that they don't have the same prototype: the member function has an implicit parameter: the this parameter (that allows the function to know to which instance of the class it belongs to). To solve the problem, you can declare the function as static (in that case, the this parameter is not passed). But of course, you won't be able to access members of the class which are non-static (because the function is global to all instances and thus doesn't belong to a specific instance). Another solution is to pass the pointer to a global function and pass the pointer to the instance of your class as the parameter (this). In the function, you'll then need to cast it to your class:
unsigned __stdcall MyGlobalFunct(void* param)
{
CMyClass* pClass = (CMyClass*)param;
pClass->MyMemFunction();
return 0;}
Cédric Moonen Software developer
Charting control [Updated - v1.1]