Passing parameters to CreateThread()
-
suppose i have a class as follows: class X { HANDLE hThread; public: X(); UINT ThreadProc(LPVOID lpVoid); }; now in the constructor X(), i call the function CreateThread() as follows: hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,NULL,0,NULL); i get the following error: error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE' Even if i do the following, i get the same error hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(X::ThreadProc),NULL,0,NULL); I dont want to use a global function ThreadProc() because i want to access data members of the current object of the class X. So even using friend wont help in this case. Even passing of parameter to ThreadProc LPVOID parameter results in certain type incompatibilities. Any workarounds ?? Please Help.. Nikhil
-
suppose i have a class as follows: class X { HANDLE hThread; public: X(); UINT ThreadProc(LPVOID lpVoid); }; now in the constructor X(), i call the function CreateThread() as follows: hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,NULL,0,NULL); i get the following error: error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE' Even if i do the following, i get the same error hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(X::ThreadProc),NULL,0,NULL); I dont want to use a global function ThreadProc() because i want to access data members of the current object of the class X. So even using friend wont help in this case. Even passing of parameter to ThreadProc LPVOID parameter results in certain type incompatibilities. Any workarounds ?? Please Help.. Nikhil
Hello Nikhil, It is not possible to use a member function as the Thread Startup function in a call to CreateThread(). CreateThread() expects either a global function or a class static function. The "static" keyword must be used when declaring your thread startup function, e.g. : class X { protected : HANDLE hThread; public: X(); protected : static DWORD WINAPI ThreadProc(LPVOID lpParameter); }; By being attributed as "static", ThreadProc() is actually a global function albeit it is within the X namespace and you may also set it as a protected function so that it cannot be accessed outside the X namespace. One word of advise, Nikhil : avoid creating your thread inside your constructor. It is not advisable because of problems which may occur due to the order of object creation. Another complication which may arise is when virtual functions get involved. These are not serious concerns if your "X" class is kept simple with no class derivation and no virtual functions. For more information, you may refer to my article : "The Importance of Sequence Order in the Creation of C++ Objects" : http://www.codeproject.com/tips/OrderOfObjectCreation.asp[^] Thanks, Bio.
-
Hello Nikhil, It is not possible to use a member function as the Thread Startup function in a call to CreateThread(). CreateThread() expects either a global function or a class static function. The "static" keyword must be used when declaring your thread startup function, e.g. : class X { protected : HANDLE hThread; public: X(); protected : static DWORD WINAPI ThreadProc(LPVOID lpParameter); }; By being attributed as "static", ThreadProc() is actually a global function albeit it is within the X namespace and you may also set it as a protected function so that it cannot be accessed outside the X namespace. One word of advise, Nikhil : avoid creating your thread inside your constructor. It is not advisable because of problems which may occur due to the order of object creation. Another complication which may arise is when virtual functions get involved. These are not serious concerns if your "X" class is kept simple with no class derivation and no virtual functions. For more information, you may refer to my article : "The Importance of Sequence Order in the Creation of C++ Objects" : http://www.codeproject.com/tips/OrderOfObjectCreation.asp[^] Thanks, Bio.
Hello Nikhil, Also, when you call CreateThread(), you may pass a pointer to your X instance as follows : hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(ThreadProc),this,0,NULL); Note the "this" is passed as the 4th parameter to CreateThread(). Then, in ThreadProc(), you may cast the input parameter back to an X pointer, e.g. : DWORD WINAPI X::ThreadProc(LPVOID lpParameter) { X* pX = (X*)lpParameter; DWORD dwRet = 0; return dwRet; } Note that because ThreadProc(), although a "global" function, is within the X namespace and so may access the private or protected members of an X instance. For example : class X { protected : HANDLE hThread; private : int m_i; public: X(); protected : static DWORD WINAPI ThreadProc(LPVOID lpParameter); }; Note that m_i is "private". But your ThreadProc() function may access X::m_i : DWORD WINAPI X::ThreadProc(LPVOID lpParameter) { X* pX = (X*)lpParameter; DWORD dwRet = 0; pX -> m_i = 100; return dwRet; } Hope the above helps, Nikhil. Best Regards, Bio.
-
suppose i have a class as follows: class X { HANDLE hThread; public: X(); UINT ThreadProc(LPVOID lpVoid); }; now in the constructor X(), i call the function CreateThread() as follows: hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,NULL,0,NULL); i get the following error: error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE' Even if i do the following, i get the same error hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(X::ThreadProc),NULL,0,NULL); I dont want to use a global function ThreadProc() because i want to access data members of the current object of the class X. So even using friend wont help in this case. Even passing of parameter to ThreadProc LPVOID parameter results in certain type incompatibilities. Any workarounds ?? Please Help.. Nikhil
See the FAQ 6.1 Why can't I use a member function as a callback?[^] --Mike-- LINKS~! Ericahist updated! | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD