pass more than one parameter to _beginThreadEx

4.3k views Asked by At

Is it possible to pass more than one parameter to beginthreadex?

I know I can create a class or structure, but what if I have unrelated pieces of data that I don't want to combine into a class or structure?

Boost libraries seem to allow for multiple parameters, but how would I do multiple parameters for standard 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

There are 1 answers

2
Rob Kennedy On BEST ANSWER

Define a struct or class. Even things that appear to send separate values end up doing the same thing underneath. Your two values are related — at the very least, they're both parameters to your thread function.