windows 将参数传递给 _beginthreadex

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5968076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:50:02  来源:igfitidea点击:

Passing parameters to _beginthreadex

c++windowsmultithreading

提问by Eilidh

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.

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

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?)

为什么不能将 NULL 作为参数传递?(因为该函数无论如何都没有参数?)

Passing NULL as an arguments list worked fine with _beginthread.

将 NULL 作为参数列表传递给 _beginthread 效果很好。

回答by Anthony Williams

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

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

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

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

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

should fix the problem for you.

应该为您解决问题。

回答by Constantinius

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;
}

回答by Cory Nelson

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

您通过第四个参数传递数据。这将传递一个指向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 unsignedand use the __stdcallcalling convention -- this is the signature _beginthreadexexpects.

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

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

根据您尝试执行的操作,VC++ 中新的并发运行时功能可能比显式管理您自己的线程更易于使用。

Editin 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:

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

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

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

回答by Puppy

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

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

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

回答by Mayank

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

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* datamust be getting a NULL pointer

现在您正在将 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);

The above code will pass pointer xto 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

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