Solved

CreateThread to function in class

Posted on 2004-10-04
17
3,026 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now