How do I create a thread routine of a static member function
class Blah
{
static void WINAPI Start();
};
// ..
// ...
// ....
hThread = (HANDLE)_beginthreadex(NULL, 0, CBlah::Start, NULL, NULL, NULL);
This gives me the following error:
***error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'***
What am I doing wrong?
Sometimes, it is useful to read the error you're getting.
Let's look at what it says. For parameter three, you give it a function with the signature
void(void)
, that is, a function which takes no arguments, and returns nothing. It fails to convert this tounsigned int (__stdcall *)(void *)
, which is what_beginthreadex
expects:It expects a function which:
unsigned int
:stdcall
calling conventionvoid*
argument.So my suggestion would be "give it a function with the signature it's asking for".
Following is the compiling version:
Following are the changes required:
(1). Start() function should return unsigned int
(2). It should take a void* as the parameter.
EDIT
Deleted point (3) as per comment
The routine passed to
_beginthreadex
must use the__stdcall
calling convention and must return a thread exit code.Implementation of Blah::Start:
Later in your code you could write any of the following:
In first case
Function-to-pointer conversion
will be applied according to C++ Standard 4.3/1. In second case you'll pass pointer to function implicitly.