_beginthreadex 静态成员函数

发布于 2024-07-30 06:22:32 字数 379 浏览 6 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谜兔 2024-08-06 06:22:33
class Blah
{
    static unsigned int __stdcall Start(void*); // void* should be here, because _beginthreadex requires it.
};

传递给 _beginthreadex 的例程必须使用 __stdcall 调用约定,并且必须返回线程退出代码

Blah::Start 的实现:

unsigned int __stdcall Blah::Start(void*)
{
  // ... some code

  return 0; // some exit code. 0 will be OK.
}

稍后在代码中您可以编写以下任意内容:

hThread = (HANDLE)_beginthreadex(NULL, 0, CBlah::Start, NULL, NULL, NULL);
// or
hThread = (HANDLE)_beginthreadex(NULL, 0, &CBlah::Start, NULL, NULL, NULL);

在第一种情况下,将根据 C++ 标准 4.3/1 应用函数到指针的转换。 在第二种情况下,您将隐式地将指针传递给函数。

class Blah
{
    static unsigned int __stdcall Start(void*); // void* should be here, because _beginthreadex requires it.
};

The routine passed to _beginthreadex must use the __stdcall calling convention and must return a thread exit code.

Implementation of Blah::Start:

unsigned int __stdcall Blah::Start(void*)
{
  // ... some code

  return 0; // some exit code. 0 will be OK.
}

Later in your code you could write any of the following:

hThread = (HANDLE)_beginthreadex(NULL, 0, CBlah::Start, NULL, NULL, NULL);
// or
hThread = (HANDLE)_beginthreadex(NULL, 0, &CBlah::Start, NULL, NULL, NULL);

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.

悟红尘 2024-08-06 06:22:33
class Blah
{
  public:
    static DWORD WINAPI Start(void * args);
};
class Blah
{
  public:
    static DWORD WINAPI Start(void * args);
};
任性一次 2024-08-06 06:22:33

以下是编译版本:

class CBlah
{
public:
    static unsigned int WINAPI Start(void*)
    {
    return 0;
    }
};

int main()
{
    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &CBlah::Start, NULL, NULL, NULL);

    return 0;
}

以下是所需的更改:

(1). Start() 函数应返回 unsigned int

(2)。 它应该采用 void* 作为参数。

编辑

根据评论删除了第(3)点

Following is the compiling version:

class CBlah
{
public:
    static unsigned int WINAPI Start(void*)
    {
    return 0;
    }
};

int main()
{
    HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &CBlah::Start, NULL, NULL, NULL);

    return 0;
}

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

在梵高的星空下 2024-08-06 06:22:33
class Blah
{
    static unsigned int __stdcall Start(void *);
};
class Blah
{
    static unsigned int __stdcall Start(void *);
};
甜警司 2024-08-06 06:22:32

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

cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'

让我们看看它怎么说。 对于参数三,您给它一个带有签名 void(void) 的函数,即一个不带参数且不返回任何内容的函数。 它无法将其转换为 unsigned int (__stdcall *)(void *),这是 _beginthreadex 期望

它期望一个函数:

  • 返回一个unsigned int
  • 使用stdcall调用约定
  • 采用void*参数。

所以我的建议是“给它一个带有它要求的签名的函数”。

class Blah
{
    static unsigned int __stdcall Start(void*);
};

Sometimes, it is useful to read the error you're getting.

cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'

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 to unsigned int (__stdcall *)(void *), which is what _beginthreadex expects:

It expects a function which:

  • Returns an unsigned int:
  • Uses the stdcall calling convention
  • Takes a void* argument.

So my suggestion would be "give it a function with the signature it's asking for".

class Blah
{
    static unsigned int __stdcall Start(void*);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文