2

I am trying to design an active object of sorts that once created, essentially runs in its own thread. What I have done so far is create a class that contains a pthread instance variable, and then in the constructor for that class, it should send the pthread on its way.

Since pthread_create() takes a function parameters, I am passing it a run() function I setup in my class implementation file.

As of now, my run() function is not a part of the class, it is just sitting in the implementation file, but when I tried to compile it, I get an error saying:

"error: ‘run’ was not declared in this scope"

Now I understand why the function run() is out of scope, but would it be correct to just add run() to my active object class as a private function, or would that cause other problems if more than one of these objects existed? Heck, would it cause problems with just one of them instantiated?

Ok here is the code, I just didn't think it mattered much. Here is MyClass.hpp

class MyClass {

private:
pthread_t thread;

    ObjectManager *queue;
int error;

    // just added this, but the compiler still doesn't like it
    void *run(void *arg);

public:

    MyClass();
    ~MyClass();

void start();
}

And here is the implementation, MyClass.cpp:

#include "MyClass.hpp"

void MyClass::start() {
if (queue == NULL)
    return;

int status = pthread_create(&thread, NULL, run, (void *) queue);
if (status != 0) {
    error = status;
    return;
}

}


void *MyClass::run(void *arg) {
bool finished = false;
while (!finished) {
        // blah blah blah
}
return NULL;
}
Ross Hays
  • 341
  • 4
  • 19
  • Please show the code, or it will be very hard to provide any help – nos Jun 19 '11 at 21:54
  • OK, just tried adding it as a class function and it will not compile due to type mismatch. It is looking for void *(*)(void*) and is getting void *(MyClass::)(void*). – Ross Hays Jun 19 '11 at 21:56
  • possible duplicate of [pthread Function from a Class](http://stackoverflow.com/questions/1151582/pthread-function-from-a-class) – Martin York Jun 20 '11 at 01:46

1 Answers1

2

Your compilation problem is likely that run is defined in your implementation file after you reference it in your constructor. Either move the definition of run before the constructor or insert a declaration of run before the constructor.

As for your problem making it a class member that won't work as pthread_create is looking for a non-member function. You could make it a static method on the class though but read In C++, is it safe/portable to use static member function pointer for C API callbacks? before deciding whether it safe for you to do so (not that you need to now that you know how to use your original function).

Community
  • 1
  • 1
Troubadour
  • 13,334
  • 2
  • 38
  • 57
  • Simple enough, thank you. Don't know why I missed that but thank you. – Ross Hays Jun 19 '11 at 22:05
  • 1
    This has been asked and answered so many times. And always-always the static member function comes up and yet it is still **always wrong**. – Martin York Jun 20 '11 at 01:47
  • @Martin - The member function I added in as a second attempt, the first time it was just out of order exactly as Troubadour said. I knew why the second way didn't work right after it reported the error. – Ross Hays Jun 20 '11 at 02:16
  • 1
    @Roflha: Read the question above. Using a static member function as a callback for pthreads is non portable and as such bad practice and should not be done. You are getting lucky that it actually works. – Martin York Jun 20 '11 at 03:29
  • @Martin: Thanks for pointing out the issue with class static methods. I didn't know that. – Troubadour Jun 20 '11 at 19:56
  • Ah I see what you meant by that now. Thanks for that, it's good to know. – Ross Hays Jun 20 '11 at 22:50