c++ - 将参数传递给 _beginthreadex

标签 c++ windows multithreading

我正在尝试使用 _beginthreadex 进行一些基本的并行化,并按照我给出的示例传递参数,但它不起作用。

有什么想法吗?

#include <iostream> 
#include <process.h>



void MyThread(void *data)
{
    std::cout << "Hello World!"; 
}

int main()
{
    _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL); 
    while(true);
}

编辑:

为什么传递 NULL 作为参数不起作用? (因为函数无论如何都不接受参数?)

将 NULL 作为参数列表传递与 _beginthread 配合良好。

最佳答案

您的代码中有两个错误,都与线程函数的参数无关——NULL 就可以了,正如您推测的那样。

问题出在线程函数的签名中,您得到的错误指出了这一点。首先,它必须是一个 __stdcall 函数,其次它必须返回一个 unsigned int。您的函数是 __cdecl 并返回 void

unsigned __stdcall MyThread(void *data)
{
    std::cout << "Hello World!"; 
    return 0;
}

应该可以为您解决问题。

关于c++ - 将参数传递给 _beginthreadex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5968076/

相关文章:

multithreading - Perl 中的读写锁

c++ - 如何设计实时 CUDA 金融应用程序

c - 在 C 中执行 shellcode (visual studio 2017

c++ - 在 std::bind 上使用 std::apply

windows - 在 go lang 中使用 exec.command 进行网络使用

windows - 为 Windows 构建 R 包时出现奇怪的错误

java - 我的应用程序卡住,如何在其中实现线程?

linux - pthread_cond_destroy() 处的死锁

c++ - 如何从 C 类中告诉/强制转换 C++ 接口(interface)的实例

c++ - 如何从 C++ 源文件生成 UML 类图?