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;
}
3
  • Please show the code, or it will be very hard to provide any help Commented Jun 19, 2011 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). Commented Jun 19, 2011 at 21:56
  • possible duplicate of pthread Function from a Class Commented Jun 20, 2011 at 1:46

1 Answer 1

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).

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

6 Comments

Simple enough, thank you. Don't know why I missed that but thank you.
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 - 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.
@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: Thanks for pointing out the issue with class static methods. I didn't know that.
|

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.