tags:

views:

61

answers:

1

I am playing with C++ and pthreads and so far so good. I can access a class member function if it's static and I've read that I can access a normal class member functions if I pass "this" as an argument with pthread_create, because c++ does this to, under the hood. But my problem is that I want to give an int to that function, and I don't know how to do multiple arguments with pthread_create.

+4  A: 

Pass a struct pointer.

struct Arg {
  MyClass* _this;
  int      another_arg;
};

...

Arg* arg = new Arg;
arg->_this = this;
arg->another_arg = 12;
pthread_create(..., arg);
...
delete arg;
KennyTM
What do I do with the _this in the c++ function? Nothing?
vincentkriek
@user: To "access a normal class member functions".
KennyTM
I think I was unclear, to let pthread_create execute c++ functions they need to be static. I would like them not to be static, if that is possible.
vincentkriek
@vincentkriek: The functions can be non-static, but you need to make sure `this` is not thread-local (and doesn't live on the stack).
KennyTM