I am trying to create a thread in the following code but the pointer to a function parameter of the pthread_create method call is just not letting me compile my code.
Please let me know what I am doing wrong and how can I fix it in the following code:
#include "RobotNodes.cpp"
int main(int argc, char** argv){
int i, numRobotsToInit = 7;
//declare run function pointer
void (*run)();
//create array of robot nodes
RobotNodes* robots[numRobotsToInit];
//init robot nodes
for(i = 0; i<numRobotsToInit; i++){
robots[i] = new RobotNodes(i, 0.2, 0.2);
}
for(i = 0; i<numRobotsToInit; i++){
run = &robots[i]->run;
pthread_t thread;
pthread_create(&thread, NULL, (void*(*)(void*))run, NULL);
}
}
The error that I get is the following:
error: lvalue required as unary ‘&’ operand
Edit: run() is a method from class RobotNodes.cpp that is included on the top of this class.
There seems to be a non-static member function in the class
RobotNodesand you seem to think that the type of member function isvoid (*)(). If so, then you are wrong.The type of non-static member functon and free function are not same, even if they have exactly same signature!
So I would suggest you to define a static function called
start, as:Then use
startas :Also, notice that I have created a vector of
pthread_toutside the loop; it is because each thread instance has to be different if they are different thread, and furthermore, each thread instance must continue to exist even after the loop stops.