[C++] Problem with threading

05/17/2009 14:30 tanelipe#1
Actually the problem is more like with passing a function as pointer to the CreateThread or _beginthreadex,

PHP Code:
BOOL Connect()
    {
        if(
m_Enabled != TRUE)
            return 
FALSE;

        
sockaddr_in addr;
        
addr.sin_family AF_INET;
        
addr.sin_port htons(m_RemotePort);
        
addr.sin_addr.S_un.S_addr inet_addr(m_RemoteIP);
        if(
connect(m_Connection, (sockaddr*)&addrsizeof(addr)) == INVALID_SOCKET)
        {
            
WSACleanup();
            
m_Enabled FALSE;
            return 
FALSE;
        }
        
hRecvThread = (HANDLE)_beginthreadex(NULL0, &AsyncRecvthisNULLNULL);
        return 
TRUE;
    }
    
unsigned __stdcall AsyncRecv(voidpArgs) {

        return 
0;
    } 
I'm not sure even if the AsyncRecv is declared right, it's a example that I found on the net. The error I'm getting is
PHP Code:
error C2276'&' illegal operation on bound member function expression 
Help is much appreciated. :p
05/17/2009 16:44 clintonselke#2
For function pointers u don't need the &. You only need to drop its () and parameters to make it a pointer. (But it should still be working w/ the & for most compliers), Also make sure u prototype your function if ur gonna use it in a line before the line in which it is defined.
05/17/2009 16:55 tanelipe#3
I got it working now >_>

PHP Code:
BOOL Connect()
    {
        if(
m_Enabled != TRUE)
            return 
FALSE;

        
sockaddr_in addr;
        
addr.sin_family AF_INET;
        
addr.sin_port htons(m_RemotePort);
        
addr.sin_addr.S_un.S_addr inet_addr(m_RemoteIP);
        if(
connect(m_Connection, (sockaddr*)&addrsizeof(addr)) == INVALID_SOCKET)
        {
            
WSACleanup();
            
m_Enabled FALSE;
            return 
FALSE;
        }
        
hRecvThread CreateThread(NULL0, (LPTHREAD_START_ROUTINE)AsyncRecvthisNULLNULL);
        return 
TRUE;
    }
    static 
DWORD WINAPI AsyncRecv(voidpArg) {
        
ClientSocketclient = (ClientSocket*)pArg;
        while(
client->m_Enabled)
        {
            
char buffer[4096];
            
int len recv(client->m_Connectionbuffersizeof(buffer), 0);    
            if(
len 0
            {
                
client->m_Enabled FALSE;
            }
            if(
OnClientReceive != NULL)
                
OnClientReceive(clientbuffer);
            
Sleep(1);
        }
        
CloseHandle(client->hRecvThread);
        return 
0;
    }