1

To define my thread I have in my Header file:

class HttpClient
{
public:
    ...
    unsigned int __stdcall  PerformLogin(void*);
    ...

};

Then in my cpp file I have:

unsigned int __stdcall PerformLogin(void*){
...
}

And to call this thread I use

hThread = (HANDLE)_beginthreadex( NULL, 0, &PerformLogin, NULL, 0, &threadID );

But i Have an error on the &PerformLogin saying that:

the args of type unsigned int (__stdcall HttpClient::)(void) is not compatible with the param unsigned int (__stdcall*)(void*).

I understand the error, but I really don't know how to fix this!

hmjd
  • 120,187
  • 20
  • 207
  • 252
darkheir
  • 8,844
  • 6
  • 45
  • 66
  • You're missing the `HttpClient::` at the beginning of `PerformLogin`'s definition. You'll still get an error after you add it (as @hmjd) describes, though, but at least your class will be defined correctly. – Eran Jul 02 '12 at 08:15
  • @darkheir: see http://stackoverflow.com/questions/1259815/beginthreadex-static-member-function for sample code of using `_beginthreadex` with member functions. – tenfour Jul 02 '12 at 10:27

3 Answers3

2

A possible way to fix this would be to make the member function static, though this means PerformLogin() does not have a this pointer and would have no access to non-static members of HttpClient.

Another is to move PerformLogin() out of HttpClient altogether, and make it a free function.

hmjd
  • 120,187
  • 20
  • 207
  • 252
2

What I usually to is to add 'this' as the void* parameter to the static function - you can then call methods on it in the static function with a bit of casting..

Martin James
  • 24,453
  • 3
  • 36
  • 60
0

Member functions get this pointer implicitly as a first parameter. So if you want to start a thread with a class member function, you should explicitly pass a pointer to a class instance in your call to _beginthreadex.

So, remove explicit argument:

class HttpClient
{
    public:
    ...
    unsigned int __stdcall  PerformLogin();
    ...
};

And call _beginthreadex while passing this as an argument:

hThread = (HANDLE)_beginthreadex( NULL, 0, &PerformLogin, this, 0, &threadID );

It's worth mentioning that this is a bit hacky. C++ FAQ advises against it. I still prefer this approach. Of course I just use boost::thread usually.

Vsevolod Golovanov
  • 4,068
  • 3
  • 31
  • 65