Solved

CreateThread to function in class

Posted on 2004-10-04
17
3,062 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:eric_cartman_16
  • 5
  • 5
  • 4
  • +2
17 Comments
 

Expert Comment

by:SirAthos
ID: 12223502
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
 

Author Comment

by:eric_cartman_16
ID: 12224104
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
 
LVL 3

Expert Comment

by:akalmani
ID: 12224549
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12224841
>>>> ..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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 200 total points
ID: 12224851
Correction:

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

turns to

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

Regards, Alex
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12225157
...Would it not be safer to use beginthreadex not CreateThread ??
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12225327
>>>> 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
 

Author Comment

by:eric_cartman_16
ID: 12225344
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
 

Expert Comment

by:SirAthos
ID: 12225367
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
 

Expert Comment

by:SirAthos
ID: 12225372
(the function is called _beginthreadex and you can find it on MSDN)
0
 

Author Comment

by:eric_cartman_16
ID: 12225417
r there any advantages gained by using WINAPI?
0
 

Assisted Solution

by:SirAthos
SirAthos earned 50 total points
ID: 12225434
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
 

Author Comment

by:eric_cartman_16
ID: 12225454
ok thankz i think i will use it
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12225458
>>>> 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
 

Author Comment

by:eric_cartman_16
ID: 12225472
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
 
LVL 3

Expert Comment

by:akalmani
ID: 12245367
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12246015
@akalmani: sorry, my fault. I should have read your answer more thoroughly.

Regards, Alex
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question