将参数传递给 _beginthreadex

发布于 2024-11-06 17:46:53 字数 422 浏览 0 评论 0原文

我正在尝试使用 _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 配合得很好。

I'm attempting to do some basic parallelisation using _beginthreadex, and passing parameters as per an example I was given, but it won't work.

Any ideas?

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



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

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

EDIT:

Why won't passing NULL as an argument work? (Since the function takes no arguments anyway?)

Passing NULL as an arguments list worked fine with _beginthread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

笨死的猪 2024-11-13 17:46:53

您的代码中有两个错误,这两个错误都与线程函数的参数无关 --- 正如您所猜测的那样, NULL 对此没有问题。

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

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

应该可以为你解决问题。

Your code has two errors in it, neither of which are related to the parameter to the thread function --- NULL is fine for that, as you surmised.

The problems are in the signature of the thread function, and the error you are getting points this out. Firstly, it must be a __stdcall function, and secondly it must return an unsigned int. Your function is __cdecl and returns void.

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

should fix the problem for you.

似梦非梦 2024-11-13 17:46:53

显然在您的代码中您没有传递任何参数。要传递变量,您必须执行以下操作(例如):

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

void MyThread(void *data)
{
    int x = static_cast<int*>(data);
    std::cout << "Hello World! " << x; 
}

int main()
{
    int x = 10;
    _beginthreadex(NULL, 0, MyThread, &x, 0, NULL); 
    while(true);
}

更新:因为您稍后发布了编译问题:
显然你的线程函数需要返回一个整数:

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

obviously in your code you are not passing any parameter. To pass a variable you have to do the following (for example):

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

void MyThread(void *data)
{
    int x = static_cast<int*>(data);
    std::cout << "Hello World! " << x; 
}

int main()
{
    int x = 10;
    _beginthreadex(NULL, 0, MyThread, &x, 0, NULL); 
    while(true);
}

UPDATE: Since you posted the compilation problem later:
apparently your thread function needs to return an integer:

int MyThread(void *data)
{
    std::cout << "Hello World!"; 
    return 0;
}
多彩岁月 2024-11-13 17:46:53

您通过第四个参数传递数据。这传递了一个指向 i 的指针:

unsigned __stdcall thread(void *arg)
{
    int *iptr = (int*)arg;
    ...
}

int i;
_beginthreadex(0, 0, thread, &i, 0, 0);

请注意,我在这里使用的线程函数签名与您使用的不同:我返回一个 unsigned 并使用 __stdcall 调用约定 - 这是 _beginthreadex 期望的签名。

根据您想要执行的操作,新的并发运行时功能在 VC++ 中使用可能比显式管理自己的线程更简单。

编辑响应问题编辑:

您可以传递任何有效的 void 指针,包括 NULL。如果这样做,您甚至可以省略参数的名称,因为您没有使用它:

unsigned __stdcall thread(void*)
{
    ...
}

_beginthreadex(0, 0, thread, 0, 0, 0);

You pass your data through the fourth parameter. This passes a pointer to i:

unsigned __stdcall thread(void *arg)
{
    int *iptr = (int*)arg;
    ...
}

int i;
_beginthreadex(0, 0, thread, &i, 0, 0);

Note that the thread function signature I used here is different than what you use: I return an unsigned and use the __stdcall calling convention -- this is the signature _beginthreadex expects.

Depending on what you're trying to do, the new Concurrency Runtime features in VC++ might be simpler to use than explicitly managing your own threads.

Edit in response to question edit:

You can pass any valid void pointer, including NULL. If you do that, you can even leave out the parameter's name since you're not using it:

unsigned __stdcall thread(void*)
{
    ...
}

_beginthreadex(0, 0, thread, 0, 0, 0);
有木有妳兜一样 2024-11-13 17:46:53

_beginthreadex 的参数必须是具有 __stdcall 调用约定的函数。您的函数有 __cdecl。在正确的位置简单插入 __stdcall 就可以解决问题。

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

The argument to _beginthreadex must be a function with the __stdcall calling convention. Your function has __cdecl. A simple insertion of __stdcall at the correct spot should solve the problem.

void __stdcall MyThread(void *data)
{
    std::cout << "Hello World!"; 
}
东走西顾 2024-11-13 17:46:53

_beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) 中的第四个参数是函数 MyThread 的参数

现在您将 NULL 传递给它,因此 void* data必须获取 NULL 指针

struct X  
{  
    int a;  
    int b;  
};

X* x = (X*)malloc(sizeof(X));  

x->a = 5;  
x->b = 6;  
_beginthreadex(NULL, 0, MyThread, NULL, x, NULL);

上面的代码将把指针 x 传递给函数 MyThread()
一定要照顾好 X,因为它最好由 malloc 或 new 分配。在使用线程连接之前,不要在堆栈上创建它

The fourth parameter in _beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) are arguments to the function MyThread

Right now you are passing NULL to it so void* data must be getting a NULL pointer

struct X  
{  
    int a;  
    int b;  
};

X* x = (X*)malloc(sizeof(X));  

x->a = 5;  
x->b = 6;  
_beginthreadex(NULL, 0, MyThread, NULL, x, NULL);

The above code will pass pointer x to the function MyThread().
Do take care of X, as it better should be allocated by malloc or new. Do not create it on stack until you use a thread join

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