将参数传递给 _beginthreadex
我正在尝试使用 _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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的代码中有两个错误,这两个错误都与线程函数的参数无关 --- 正如您所猜测的那样,
NULL
对此没有问题。问题出在线程函数的签名中,您收到的错误指出了这一点。首先,它必须是一个
__stdcall
函数,其次它必须返回一个unsigned int
。您的函数是__cdecl
并返回void
。应该可以为你解决问题。
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 anunsigned int
. Your function is__cdecl
and returnsvoid
.should fix the problem for you.
显然在您的代码中您没有传递任何参数。要传递变量,您必须执行以下操作(例如):
更新:因为您稍后发布了编译问题:
显然你的线程函数需要返回一个整数:
obviously in your code you are not passing any parameter. To pass a variable you have to do the following (for example):
UPDATE: Since you posted the compilation problem later:
apparently your thread function needs to return an integer:
您通过第四个参数传递数据。这传递了一个指向
i
的指针:请注意,我在这里使用的线程函数签名与您使用的不同:我返回一个
unsigned
并使用__stdcall 调用约定 - 这是 _beginthreadex 期望的签名。
根据您想要执行的操作,新的并发运行时功能在 VC++ 中使用可能比显式管理自己的线程更简单。
编辑响应问题编辑:
您可以传递任何有效的 void 指针,包括 NULL。如果这样做,您甚至可以省略参数的名称,因为您没有使用它:
You pass your data through the fourth parameter. This passes a pointer to
i
: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:
_beginthreadex 的参数必须是具有 __stdcall 调用约定的函数。您的函数有
__cdecl
。在正确的位置简单插入 __stdcall 就可以解决问题。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._beginthreadex(NULL, 0, MyThread, NULL, 0, NULL) 中的第四个参数是函数 MyThread 的参数
现在您将 NULL 传递给它,因此
void* data
必须获取 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 pointerThe above code will pass pointer
x
to the functionMyThread()
.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