Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is mine base class
C++
 class IDialysisConnector
{
public:
HANDLE threadHandle_;   
virtual int ConnectToMachine();  //This will make socket connection with the machine.
virtual void WINAPI ServerConnectThread(LPVOID lpdwThreadParam)=0;
};

Another class
C++
class A:public IDialysisConnector
{

int ConnectToMachine()
{
DWORD dwThreadId;
threadHandle_=CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)ServerConnectThread,(LPVOID)sock_,0,&dwThreadId);
void WINAPI IDialysisConnector::ServerConnectThread(LPVOID lpdwThreadParam)
	{
printf"this is done");
        }

}

}



I did like this too but same error
C++
threadHandle_=CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)IDialysisConnector::ServerConnectThread,(LPVOID)sock_,0,&dwThreadId);

I am getting an error that is
" error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE'"
Posted

You may use only a static class member function as (third) argument to CreateThread (see ThreadProc callback function[^]).
If you need to access class instance members from the thread routine the you may pass object's this pointer as thread function parameter.

[UPDATE]
change your function this way:
C++
// inside class A declaration:
//..
  DWORD WINAPI ServerConnectThread(LPVOID lpdwThreadParam);
// ..
// in class A implementation:
//..
static DWORD WINAPI IDialysisConnector::ServerConnectThread(LPVOID lpdwThreadParam)
{
  printf"this is done");
  return 0;
}
//..


[/UPDATE]
 
Share this answer
 
v2
Comments
Tarun Batra 11-Oct-12 9:28am    
So how should i modify the code?
Albert Holguin 11-Oct-12 10:15am    
Pretty self-explanatory... declare it as static...
Albert Holguin 11-Oct-12 10:30am    
+5...
CPallini 12-Oct-12 11:42am    
Thank you.
Nelek 12-Oct-12 10:58am    
Nice answer. +5
just a small correction to CPallini's code: you need to declare as static.

C++
// inside class A declaration:
class A: public IDialysisConnector:
//..
static DWORD WINAPI ServerConnectThread(LPVOID lpdwThreadParam);
// ..


// in class A implementation:
//..
DWORD WINAPI IDialysisConnector::ServerConnectThread(LPVOID lpdwThreadParam)
{
  printf"this is done");
  return 0;
}
//..


obviously, you can declare virtual static functions. Therefore you need to delete the pure virtual function from IDialysisConnector. So IDialysisConnector should be:

C++
 class IDialysisConnector
{
public:
HANDLE threadHandle_;   
virtual int ConnectToMachine();  //This will make socket connection with the machine.
};
 
Share this answer
 
You may create your own wrapper, like this:
C++
///////////////////////////////////////////////////////////////////////////////////
class CThread
{
private:
    static DWORD WINAPI CThreadFunc(_In_  LPVOID lpParameter)
    {//connection to a class
     //needed to have access to nonstatic members
        reinterpret_cast<CThread*>(lpParameter)->run();
        return something;
    }
protected:
    virtual void run() = 0;
public:
    void start()
    {//threading routines wrapper
        DWORD dwThreadId;
        CreateThread(NULL,0, CThreadFunc,(LPVOID)this,0,&dwThreadId);
    }
}


After that, you can use your own threads
C++
class CMyServerThread: public CThread
{
protected:
    virtual void run()
    {
        // do anything you like there, 
        // this will be executed in separated thread
        // here you have full access to non static members of the class
    }
public:
}

After that, you can use your own threads,
start like this:
C++
CMyServerThread myServerThread;
myServerThread.start();
 
Share this answer
 
v4
Use static function or global function for creating thread.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900