Jump to content
  • Advertisement
Sign in to follow this  
DarkRonin

Problems creating a thread within a class.

This topic is 2698 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!