-2

How can I pass args to this function:

int myClass::myFunc(void * aArgs){

    return 0;
}

I call it this way:

thrd_create(&t, myClass::myFunc, (void*)0);

I need to pass multiple arguments to the function, how can I achieve it?

ProtectedVoid
  • 1,293
  • 3
  • 17
  • 42
  • Well then create a struct which acts as your parameter storage and pass this struct to the function. – ckruczek Jul 15 '15 at 07:49
  • _"I need to pass multiple arguments to the function, how can I achieve it?"_ Use a `struct` and pass the address of an instance of that one. – πάντα ῥεῖ Jul 15 '15 at 07:50
  • possible duplicate of [How to pass multiple parameters to a thread in C](http://stackoverflow.com/questions/8223742/how-to-pass-multiple-parameters-to-a-thread-in-c) – Mohit Jain Jul 15 '15 at 07:52
  • If you have C++11, you might have the option of `std::thread`, which is standard and makes it very easy to pass any number of arguments while maintaining a regular function signature (no `void *`). – chris Jul 15 '15 at 07:54
  • @chris I don't have C++11, using Visual Studio 2005 compiler. Thanks. – ProtectedVoid Jul 15 '15 at 07:56
  • @ProtectedVoid, I'm not sure offhand whether Boost supports 2005, but `boost::thread` acts very similarly to `std::thread` without need needing C++11 and Boost is at least a de facto standard in many cases. – chris Jul 15 '15 at 07:59
  • @chris I've already tried Boost and it isn't compatible with the tool i'm working with. I'm using TinyCThread at the moment. Thanks for your support. – ProtectedVoid Jul 15 '15 at 08:09

1 Answers1

4

There are plenty of ways. For example:

struct Arg_Struct
{
  int m_nArg1;
  float m_fArg2;
  bool m_bArg3;
}
//...
Arg_Struct* pStruct = new Arg_Struct;
pStruct->m_nArg1 = 0;
thrd_create(&t, myClass::myFunc, (void*)pStruct);

//...

int myClass::myFunc(void * aArgs){
   Arg_Struct* pArgs = (Arg_Struct*)aArgs;
   int n = pArgs->m_nArg1;
//...
   return 0;
}
Ari0nhh
  • 5,720
  • 3
  • 28
  • 33