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
eric_cartman_16Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
itsmeandnobodyelseConnect With a Mentor Commented:
Correction:

>>> DWORD WINAPI static init(LPVOID param)
>>> {

turns to

DWORD WINAPI LOCAL_LISTEN::init(LPVOID param)
{
   ...

Regards, Alex
0
 
SirAthosCommented:
Try defining your init function as LPTHREAD_START_ROUTINE, that is:
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.
0
 
eric_cartman_16Author Commented:
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);}
}
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
akalmaniCommented:
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);}
}
0
 
itsmeandnobodyelseCommented:
>>>> ..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




0
 
mrwad99Commented:
...Would it not be safer to use beginthreadex not CreateThread ??
0
 
itsmeandnobodyelseCommented:
>>>> 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

0
 
eric_cartman_16Author Commented:
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
0
 
SirAthosCommented:
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. :)
0
 
SirAthosCommented:
(the function is called _beginthreadex and you can find it on MSDN)
0
 
eric_cartman_16Author Commented:
r there any advantages gained by using WINAPI?
0
 
SirAthosConnect With a Mentor Commented:
Depends on how you see it. I find the WINAPI functions easier to use sometimes.
Other than that, I believe that not using them adds some overhead, as you will call a C lib function instead of the OS function directly.
0
 
eric_cartman_16Author Commented:
ok thankz i think i will use it
0
 
itsmeandnobodyelseCommented:
>>>> 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

0
 
eric_cartman_16Author Commented:
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
0
 
akalmaniCommented:
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.
0
 
itsmeandnobodyelseCommented:
@akalmani: sorry, my fault. I should have read your answer more thoroughly.

Regards, Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.