将多个参数传递给_beginThreadEx

时间:2012-10-17 13:37:00

标签: c++ windows multithreading parameters beginthreadex

是否可以将多个参数传递给beginthreadex?

我知道我可以创建一个类或结构,但是如果我有不相关的数据片段,我不想将它们组合成一个类或结构呢?

Boost库似乎允许多个参数,但我如何为标准c ++ _beginThreadEx做多个参数?

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

unsigned __stdcall myThread(void *data)
{
    //C:\dev\default threads\_threads.cpp|6|error: invalid conversion from 'int*' to 'int' [-fpermissive]|
    int *x = static_cast<int*>(data);

    //int *x = (int*)data;

    std::cout << "Hello World! " << x;
}

int main()
{

    int x = 10;
    _beginthreadex(NULL, 0, myThread, &x, 0, NULL);
    while(true);
}

1 个答案:

答案 0 :(得分:6)

定义结构或类。即使看起来发送单独值的事情最终会在下面做同样的事情。你的两个值相关的 - 至少,它们都是你的线程函数的参数。