_beginthreadex静态成员函数

aki*_*kif 1 c++ static multithreading function member

如何创建静态成员函数的线程例程

class Blah
{
    static void WINAPI Start();
};

// .. 
// ...
// ....

hThread = (HANDLE)_beginthreadex(NULL, 0, CBlah::Start, NULL, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

***error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'***
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

jal*_*alf 16

有时,阅读您收到的错误很有用.

cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'
Run Code Online (Sandbox Code Playgroud)

让我们来看看它的内容.对于参数三,你给它一个带签名的函数void(void),即一个不带参数的函数,什么都不返回.它无法将其转换为unsigned int (__stdcall *)(void *),这是_beginthreadex 预期的:

它期望一个功能:

  • 返回一个unsigned int:
  • 使用stdcall调用约定
  • 采取void*争论.

所以我的建议是"用它要求的签名给它一个功能".

class Blah
{
    static unsigned int __stdcall Start(void*);
};
Run Code Online (Sandbox Code Playgroud)