Thread errors

Started by
4 comments, last by Oluseyi 19 years, 7 months ago
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
____________________________________________________________Programmers Resource Central
Advertisement
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 functionif( (threadHandle = CreateThread(NULL, 0, NetworkServer::acceptingProcedure_Static, (LPVOID)this, 0, (LPDWORD)&threadID)) == NULL){//...}
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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 //parametersboost::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
While fixing your grammar error, MonkeyCookie, perhaps you should have read the post before yours? Just a thought. [smile]
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.
____________________________________________________________Programmers Resource Central
See Precompiled headers.

This topic is closed to new replies.

Advertisement