2

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);
}
Mysticial
  • 464,885
  • 45
  • 335
  • 332
thistleknot
  • 1,098
  • 16
  • 38

1 Answers1

7

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • so in other words, NO you cannot pass more than one argument to a _beginThreadEx, one must use the functions parameter listing of one argument. Therefore, being forced to use a struct or class. This is what I was hoping to find out, as I stated, boost threads work differently (you can send more than one parameter). Which means I will have to rethink the way I pass data to my functions for threaded functions. – thistleknot Oct 17 '12 at 16:49
  • Of course, your struct or class can contain pointers rather than the actual data if that's a more convenient way of doing things. This will be how Boost handles multiple parameters, it's just hiding the details from you. – Harry Johnston Oct 17 '12 at 19:36