1

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.

1
  • Are you sure you're not confusing free functions with member functions? Commented Oct 19, 2011 at 11:41

3 Answers 3

4

There seems to be a non-static member function in the class RobotNodes and you seem to think that the type of member function is void (*)(). 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:

class RobotNodes
{
   public:
        void run(); //define it as you like

        static void* start(void *pdata)
        {
             RobotNodes *robot = static_cast<RobotNodes*>(pdata);
             robot->run(); //forward it
             return 0; //or return as the documentation says
        }
};

Then use start as :

std::vector<pthread_t> threads(numRobotsToInit);
for(i = 0; i<numRobotsToInit; i++)
{
    pthread_create(&threads[i], NULL, RobotNodes::start, &robots[i]);       
}

Also, notice that I have created a vector of pthread_t outside 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.

Sign up to request clarification or add additional context in comments.

Comments

0

the problem is that you are attempting to specify a pointer to member function, rather than a standard function.

Instead, pass the robot as the parameter to a function which tells the robot to run:

extern "C" void* run_robot(void* robot) __attribute__((noreturn));
void* run_robot(void* robot) {
    RobotNodes* node(static_cast<RobotNodes*>(robot));
    node->run();
    pthread_exit(0);
}
...
pthread_create(&thread, NULL, run_robot, &robots[i]);

Comments

0

Where is run defined?

Using pthread_create(&thread, ...) is bad since thread a variable local to the for loop and will go out of scope at the end of the for.

try instead:

for(i = 0; i<numRobotsToInit; i++){
  run = &robots[i]->run;
  pthread_t* thread = new pthread_t;  //you will get a memory leak unless you store these to delete later
  pthread_create(thread, NULL, run, NULL);       
}

If i understood correctly the run function is a member of RobotNodes, in this case you need to make a dispatcher to call this function since you cannot pass member functions the pthread_create.

Create a static function called dispatch or something, pass it a pointer to the class instance you want to invoke the function on.

1 Comment

Sorry forgot to include that run() is from another class(RobotNodes) that is included in this class. Get the following errors when I run the code with your changes: error: cannot convert ‘void (RobotNodes::*)()’ to ‘void ()()’ in assignment error: invalid conversion from ‘void ()()’ to ‘void* ()(void)’ error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)’

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.