Sign in to follow this  

Thread errors

This topic is 4841 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

I've got a thread in a class and I keep getting compile errors. I get the errors when I call CreateThread. Here's the code:
class NetworkServer{
...
public:
	void acceptingProcedure();
...
};

void NetworkServer::acceptingProcedure()
{
...
}

// The error is with this function
if( (threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)acceptingProcedure, NULL, 0, (LPDWORD)&threadID)) == NULL)
{
...
}

This is the error I get: error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type Does anyone have any idea of what's wrong? Tera_Dragon

Share this post


Link to post
Share on other sites
Your thread function must be of the form
DWORD WINAPI FunctionName(LPVOID lpParameter);

Note that it also must be a static function, if it is part of a class; member functions cannot be used. Here's what I often do:

class NetworkServer
{
//...
public:
static DWORD WINAPI acceptingProcedure_Static(LPVOID lpParameter);
void acceptingProcedure();
//...
};

DWORD WINAPI NetworkServer::acceptingProcedure_Static(LPVOID lpParameter)
{
if (lpParameter != NULL)
{
((NetworkServer*)lpParameter)->acceptingProcedure();
return 0;
}
else
{
return -1;
}
}

void NetworkServer::acceptingProcedure()
{
//...
}

// The error is with this function
if( (threadHandle = CreateThread(NULL, 0, NetworkServer::acceptingProcedure_Static, (LPVOID)this, 0, (LPDWORD)&threadID)) == NULL)
{
//...
}

Share this post


Link to post
Share on other sites
Your acceptingProcedure is the wrong type of function. Check out this MSDN page for an example on what a thread function looks like and how to use it: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/creating_threads.asp

In addition, I doubt that giving it a member function will work. Member functions need an object pointer to go along with them in order to be called from elsewhere, since they are able to work on data local to a particular object. The Win32 API is a C API and has no concept of objects, so there is no way to give it an object pointer.

You'll have to put your thread code in a non-member function. If you want to operate on a particular object in your thread, I would suggest placing the object somewhere that is accessable to your thread function (in a static data structure or something) and then retrieving it and working with it in the thread function.

Alternately, there are other thread libraries out there. I know the Boost C++ library has a thread library. A boost::thread accepts a boost::function, which can be created from an object pointer and member function via boost::bind.

So you can create a boost::thread and pass it your member function like this:


//Create your thread
//Class::member function has a void return type and accepts no //parameters
boost::thread myThread(boost::bind(&Class::memberFunction, &object);

//myThread will run until it is out of scope.




I've never actually used boost::thread before, so my code may not be 100% accurate. I've used other boost libraries in the past, and they work great. I do plan to use boost.thread in my current project, but I haven't reached that part yet.

I know that SDL also has thread functionality, so you can check that out too if you like.

EDIT: Fixed grammar error

Share this post


Link to post
Share on other sites
Thanks for the help, it looks like that is working now.
However I know have a problem with the client. I'm getting this message:
error C2143: syntax error : missing ';' before 'PCH creation point'

I get this on the line of my main() function. I had this same error a while ago, it caused me lots of trouble and no one could help me. The only problem is that I can't remember how I solved it (or even if I did)


EDIT: I'm a fool. I just missed a colon, lol.

Share this post


Link to post
Share on other sites

This topic is 4841 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this