views:

211

answers:

2

I am trying to create a class for network programming. This will create a general purpose socket with thread.

But when I tried to creat the thread using createthread(). The third argument is producing errors. And from the net I came to know that I can't use the member functions as a argument to the createthread().

Is there any thing by which I can achieve this.

+3  A: 

The easiest way to handle this is to create a "stub" function which calls back into your class.

UINT tid
HANDLE hThread = CreateThread(NULL, 0, myThreadStub, this, 0, &tid);

....

unsigned long WINAPI myThreadStub(void *ptr) 
{
    if (!ptr) return -1;
    return ((MyClass*)ptr)->ThreadMain();
}

CreateThread() allows you to pass an argument to the thread function (parameter 4 of the CreateThread() call). You can use this to pass a pointer to your class. You can then have the thread stub cast that pointer back into the proper type and then call a member function. You can even have "myThreadStub" be a static member of "MyClass", allowing it to access private members and data.

If you have boost installed, you may be able to use boost::bind to do this without creating a stub function. I've never tried that on windows, so I can't say for sure it would work (because the callback function must be a WINAPI call) but if it does work it would look something like:

HANDLE hThread = CreateThread(NULL, 0, boost::bind(&MyClass::ThreadFunction, this), NULL, 0, &tid);

Where thread function is a non-static member function which takes a single void * argument.

SoapBox
can you please say how to post the codelike above
prabhakaran
Use the "1010" button, or just indent 4 spaces.
SoapBox
Boost.Bind doesn't help here - it returns functors, not function pointers.
Georg Fritzsche
A: 

At lost I got it, the very fact is, in CreateThread if you pass the socket then there is no trouble. Because CreateThread is taking care of that socket. But if you pass as an object which is having that socket, then CreateThread is not taking care of the socket, and it is ends up in invalid socket in the new thread.

The successed code below

SOCKET s=socket(....);
bind(s,...);
listen(s,...);
SOCKET temp=accept(s,(sockaddr *)&addrNew,&size);
DWORD threadId;
HANDLE thread=CreateThread(NULL,0,&MyThreadFunction,(LPVOID)(temp),0,&threadId);
prabhakaran
There is no reason that passing an object pointer which contains the socket as a member variable wouldn't work. If the socket variable became invalid, it is due to some coding error on your part. CreateThread has no concept of sockets, or how to "take care" of them.
Brook Miles
I don't think so. I am examining this for nearly two weeks. You can check it. My assumption is socket needs some environment, and when you are paasing it through CreateThread, createthread passes the environment also. But if you juat pass a object(which is having that socket), then in thread it is invalid. I checked it. And MSDN forum also showing examples as passing socket in createthread. It seems they are hiding some minus point of createthread.
prabhakaran