eric_cartman_16
asked on
CreateThread to function in class
Hey ya...
I am building a class which will look something like this: (simplified, just to get the idea)
class CONNECTION
{
public:
void start();
private:
void init();
HANDLE hC;
DWORD tC;
};
When start() is called, it creates a new thread which gets looped around in init();
The first code i used for start() was:
hC = CreateThread(0, 0, (LP_THREAD_START_ROUTINE) init, 0, 0, &tC);
This produced the error:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE'
So i changed the code to:
hC = CreateThread(0, 0, (void*)(void) init, 0, 0, &tC);
This appeared to work at first, but the code in init() never got called, and when i tried to call CloseHandle() on hC, it produced an exception saying invalid handle. So although it looked like it worked, it clearly did not work.
I would like to know how to achieve this
>> when start() is called, it creates a new thread which gets looped around in init();
any help would be appreciated,
SOUTHERN CURRIEZ
I am building a class which will look something like this: (simplified, just to get the idea)
class CONNECTION
{
public:
void start();
private:
void init();
HANDLE hC;
DWORD tC;
};
When start() is called, it creates a new thread which gets looped around in init();
The first code i used for start() was:
hC = CreateThread(0, 0, (LP_THREAD_START_ROUTINE) init, 0, 0, &tC);
This produced the error:
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE'
So i changed the code to:
hC = CreateThread(0, 0, (void*)(void) init, 0, 0, &tC);
This appeared to work at first, but the code in init() never got called, and when i tried to call CloseHandle() on hC, it produced an exception saying invalid handle. So although it looked like it worked, it clearly did not work.
I would like to know how to achieve this
>> when start() is called, it creates a new thread which gets looped around in init();
any help would be appreciated,
SOUTHERN CURRIEZ
ASKER
Hey thankz for replying...
I changed it to DWORD WINAPI init(LPVOID), and i still got the same typecast error, cannot convert from overloaded function to LPTHREAD_START_ROUTINE.
When i tried DWORD WINAPI static init(LPVOID) i got all kinds of crazy errors, including the original typecast error:
warning C4518: 'static ' : storage-class or type specifier(s) unexpected here; ignored
warning C4518: 'static ' : storage-class or type specifier(s) unexpected here; ignored
warning C4230: anachronism used : modifiers/qualifiers interspersed, qualifier ignored
warning C4230: anachronism used : modifiers/qualifiers interspersed, qualifier ignored
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE'
Here is the exact source code i was using:
class LOCAL_LISTEN
{
public:
LOCAL_LISTEN::LOCAL_LISTEN (SOCKET);
bool start();
private:
DWORD WINAPI static init(LPVOID);
bool quit;
HANDLE hLL;
DWORD tLL;
};
LOCAL_LISTEN::LOCAL_LISTEN (SOCKET sock)
{
quit = false;
}
bool LOCAL_LISTEN::start()
{
hLL = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) init, this, 0, &tLL);
if(hLL) return true;
return false;
}
DWORD WINAPI static init(LPVOID param)
{
while(true){
MessageBox(NULL, "init called", "LOCAL_LISTEN", 0);
Sleep(1000);}
}
I changed it to DWORD WINAPI init(LPVOID), and i still got the same typecast error, cannot convert from overloaded function to LPTHREAD_START_ROUTINE.
When i tried DWORD WINAPI static init(LPVOID) i got all kinds of crazy errors, including the original typecast error:
warning C4518: 'static ' : storage-class or type specifier(s) unexpected here; ignored
warning C4518: 'static ' : storage-class or type specifier(s) unexpected here; ignored
warning C4230: anachronism used : modifiers/qualifiers interspersed, qualifier ignored
warning C4230: anachronism used : modifiers/qualifiers interspersed, qualifier ignored
error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'LPTHREAD_START_ROUTINE'
Here is the exact source code i was using:
class LOCAL_LISTEN
{
public:
LOCAL_LISTEN::LOCAL_LISTEN
bool start();
private:
DWORD WINAPI static init(LPVOID);
bool quit;
HANDLE hLL;
DWORD tLL;
};
LOCAL_LISTEN::LOCAL_LISTEN
{
quit = false;
}
bool LOCAL_LISTEN::start()
{
hLL = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) init, this, 0, &tLL);
if(hLL) return true;
return false;
}
DWORD WINAPI static init(LPVOID param)
{
while(true){
MessageBox(NULL, "init called", "LOCAL_LISTEN", 0);
Sleep(1000);}
}
Just change your thread function like this..static keyword has to be removed. and class specifier has to be used.
DWORD WINAPI LOCAL_LISTEN::init(LPVOID param)
{
while(true){
MessageBox(NULL, "init called", "LOCAL_LISTEN", 0);
Sleep(1000);}
}
DWORD WINAPI LOCAL_LISTEN::init(LPVOID param)
{
while(true){
MessageBox(NULL, "init called", "LOCAL_LISTEN", 0);
Sleep(1000);}
}
>>>> ..static keyword has to be removed
No, the function must be defined static if used as start func of a thread.
class LOCAL_LISTEN
{
public:
LOCAL_LISTEN::LOCAL_LISTEN (SOCKET);
bool start();
private:
static DWORD WINAPI init(LPVOID);
bool quit;
HANDLE hLL;
DWORD tLL;
};
The call is ok and the implementation is like that:
DWORD WINAPI static init(LPVOID param)
{
LOCAL_LISTEN* pThis = (LOCAL_LISTEN*)param;
while(true){
MessageBox(NULL, "init called", "LOCAL_LISTEN", 0);
Sleep(1000);}
}
Regards, Alex
No, the function must be defined static if used as start func of a thread.
class LOCAL_LISTEN
{
public:
LOCAL_LISTEN::LOCAL_LISTEN
bool start();
private:
static DWORD WINAPI init(LPVOID);
bool quit;
HANDLE hLL;
DWORD tLL;
};
The call is ok and the implementation is like that:
DWORD WINAPI static init(LPVOID param)
{
LOCAL_LISTEN* pThis = (LOCAL_LISTEN*)param;
while(true){
MessageBox(NULL, "init called", "LOCAL_LISTEN", 0);
Sleep(1000);}
}
Regards, Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
...Would it not be safer to use beginthreadex not CreateThread ??
>>>> Would it not be safer to use beginthreadex not CreateThread ??
No, in my experience there is no difference in quality (in fact CreateThread is implemented using beginthreadex). The only question is if you want to use WINAPI or not. And with CreateThread you have some more options, e. g. security issues.
Regards, Alex
No, in my experience there is no difference in quality (in fact CreateThread is implemented using beginthreadex). The only question is if you want to use WINAPI or not. And with CreateThread you have some more options, e. g. security issues.
Regards, Alex
ASKER
Alex:
Nice solution. Everything works find with ur setup... I could not find a MSDN defination for beginthreadex so in my mind it is better to use CreateThread.
However could you elaborate on why/why not to use WINAPI
Regardz,
Suma
Nice solution. Everything works find with ur setup... I could not find a MSDN defination for beginthreadex so in my mind it is better to use CreateThread.
However could you elaborate on why/why not to use WINAPI
Regardz,
Suma
It's finally a question whether you want to be able to run your app on other operating systems. The WINAPI is definitely only Windows compatible. :)
(the function is called _beginthreadex and you can find it on MSDN)
ASKER
r there any advantages gained by using WINAPI?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok thankz i think i will use it
>>>> However could you elaborate on why/why not to use WINAPI
Yes, if you want a portable solution, e. g. running on Linux platforms or MAC, you may not use WINAPI. With beginthreadex you have a portable alternative to CreateThread. However, if you need WINAPI anyway then CreateThread is as good as beginthreadex.
Regards, Alex
Yes, if you want a portable solution, e. g. running on Linux platforms or MAC, you may not use WINAPI. With beginthreadex you have a portable alternative to CreateThread. However, if you need WINAPI anyway then CreateThread is as good as beginthreadex.
Regards, Alex
ASKER
gotta luv cross-posts - i saw your post immediately after i closed the question
well i dont think i will be running this on multiple platforms, because im already including "windows.h" and "winsock2.h"...
cheers,
Suma
well i dont think i will be running this on multiple platforms, because im already including "windows.h" and "winsock2.h"...
cheers,
Suma
itsmeandnobodyelse:
>>>> ..static keyword has to be removed
I meant only in the definition not declaration. I am aware that thread function has to be static or global. Just that he (asker) had typed in static in definition too and also class specifier was missing.
Anyways I am too late for this since answer has been accepted.
>>>> ..static keyword has to be removed
I meant only in the definition not declaration. I am aware that thread function has to be static or global. Just that he (asker) had typed in static in definition too and also class specifier was missing.
Anyways I am too late for this since answer has been accepted.
@akalmani: sorry, my fault. I should have read your answer more thoroughly.
Regards, Alex
Regards, Alex
DWORD WINAPI init( LPVOID lpThreadParameter );
you would have to declare it static if you want it to be a member of the class, and pass "this" as the lpParameter if you want to access non-static members.