Here is my code which contains error:
void ClassA::init()
{
HANDLE hThread;
data thread; // "thread" is an object of struct data
hThread = CreateThread(NULL, 0, C1::threadfn, &thread, 0, NULL);
}
DWORD WINAPI ClassA::threadfn(LPVOID lpParam)
{
data *lpData = (data*)lpParam;
}
Error:
error C3867: 'ClassA::threadfn': function call missing argument list; use '&ClassA::threadfn' to create a pointer to member
What is the proper way to make the worker thread working in a single class?
The thread creation functions are not aware of C++ classes; as such, your thread entry point must be either a static class member function, or a non-member function. You can pass in the
this
pointer as thelpvThreadParam
parameter to theCreateThread()
function, then have the static or non-member entry point function simply call thethreadfn()
function via that pointer.If the
threadfn()
function is static, then make sure you put&
beforeC1::threadfn
.Here's a simple example:
Follow the advice in the warning error, then this should work provided the member function
threadfn
isstatic
.What happens if you do what the error says?
This assumes that
threadfn()
is static.You're using MFC, according to the tags.
CreateThread
is the Win32 C API, you should look atCWinThread
instead.