Problems creating a thread within a class.

Started by
3 comments, last by DividedByZero 12 years, 11 months ago
Hi guys,

I am trying to create a private CreateThread function which I can't get to compile.

error C3867: 'Thread::testThread': function call missing argument list; use '&Thread::testThread' to create a pointer to member

I have Googled this but can't understand what is going on.

#include<windows.h>
#include<iostream>

class Thread
{
public:

Thread()
{
}
~Thread()
{
}

void something()
{
CreateThread(0,0,testThread,0,0,0);
}

private:
DWORD WINAPI testThread(LPVOID args)
{
while(true)
{
std::cout<<"test\r\n";
Sleep(500);
}
return 0;
}
};


If I put DWORD WINAPI testThread(LPVOID args) outside the class it compiles ok.

I also tried CreateThread(0,0,&Thread::testThread,0,0,0);

Any help would be awesome :)
Advertisement
if i remember correctly, you can't create a thread that points directly at a non-static member. Make the thread member function static, then pass 'this' in as the threads LPVOID args parameter:


#include<windows.h>
#include<iostream>

class Thread
{
public:

Thread()
{
}
~Thread()
{
}

void something()
{
CreateThread(0,0,&Thread::testThread, this, 0, 0);
}

private:
static DWORD WINAPI testThread(LPVOID args)
{
while(true)
{
std::cout<<"test\r\n";
Sleep(500);
}
return 0;
}
};


Hope this helps!
Spot on! You are awesome +1 to rep :)

Is there a general rule as to when things need to be static? This issue always gets me in trouble.
There is no general rule, except that because createthread is part of the windows API and the windows API is generally C-only, it can't interact with objects/classes unless you 'flatten' them using static first.

Thanks for letting me know that it fixed it :-)
No probs, thanks again :cool:

This topic is closed to new replies.

Advertisement