_beginthreadex 静态成员函数
如何创建静态成员函数的线程例程
class Blah
{
static void WINAPI Start();
};
// ..
// ...
// ....
hThread = (HANDLE)_beginthreadex(NULL, 0, CBlah::Start, NULL, NULL, NULL);
这给了我以下错误:
***error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'***
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
传递给
_beginthreadex
的例程必须使用__stdcall
调用约定,并且必须返回线程退出代码。Blah::Start 的实现:
稍后在代码中您可以编写以下任意内容:
在第一种情况下,将根据 C++ 标准 4.3/1 应用
函数到指针的转换
。 在第二种情况下,您将隐式地将指针传递给函数。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.以下是编译版本:
以下是所需的更改:
(1). Start() 函数应返回 unsigned int
(2)。 它应该采用 void* 作为参数。
编辑
根据评论删除了第(3)点
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
有时,阅读您收到的错误很有用。
让我们看看它怎么说。 对于参数三,您给它一个带有签名
void(void)
的函数,即一个不带参数且不返回任何内容的函数。 它无法将其转换为unsigned int (__stdcall *)(void *)
,这是_beginthreadex
期望:它期望一个函数:
unsigned int
:stdcall
调用约定void*
参数。所以我的建议是“给它一个带有它要求的签名的函数”。
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".