John John - 4 months ago 24
C++ Question

How to pass parameters and function to pthread_create

Okay so i'm looking at the documentation for pthread_create and I just don't understand at all how to do what I want to do.

I want to call pthread_create which will obv pass in a struct of pthread_t. But the function I pass to it takes in a pointer to a of MyNode*. How would I pass the function as a parameter and pass it "this" as a parameter to that function.

//MyNode field and method in class file
pthread_t myTrd;

static void compute(MyNode* node);

//Actual code in header file below
static void MyNode::compute(*MyNode node){ //L61

void MyNode::run(){ //run function in header file
pthread_create(&(this->thread),NULL,MyNode::compute, this);


myNode.cpp:61: error: 'static' may not be used when defining (as opposed to declaring) a static data member
myNode.cpp:61: error: 'int MyProjectGraph::MyNode::compute' is not a static member of 'class MyProjectGraph::MyNode'
myNode.cpp:61: error: expected primary-expression before 'node'
myNode.cpp:61: error: expected ',' or ';' before '{' token
myNode.cpp:134: error: expected `}' at end of input


The function passed to pthread_create() should match the prototype:

void *function(void *arg);

If your function does not match that, you have to use brute force and ignorance (and a cast) to make the function pointer acceptable — and then hope that the alternative interface doesn't break anything.

It is far better to make your function match the specification:

void *function(void *arg)
    MyNode *mnp = (MyNode *)arg;
    return 0;

The return can return some more meaningful value if you have one available, but returning a null (you could probably write nullptr given that you're mainly using C++).

Note that pthread_create() is usually a C function itself and expects C function semantics in the function pointer it is passed.