1

I have a little problem in my current project because i do want tu use an objects method when creating my thread. i red that it is impossible without declare this method as static. Any idea ?

  public:
        CModelisation (int argc, char **argv, char[]);
    ~CModelisation ();

    void Init ();
    void *RunMainLoop (void* args);
};

CModelisation.cpp

void *CModelisation::RunMainLoop (void* args)
{
    glutDisplayFunc(Display);
    glutIdleFunc(Display);
    glutReshapeFunc(Redisplay);
    glutMotionFunc(Mouse);
    glutKeyboardFunc(Keyboard);
    glutMainLoop();
    return args;
}

Main

    threads[1] = new CThread();
    threads[1]->exec(Model->RunMainLoop,(void*)1);

THX !

1 Answer 1

2

I believe it's common practice to create a wrapper function for any thread-method:

struct Foo {

    void someMethod() {
        // ... the actual method ...
    }
    static void* someMethodWrap(void* arg) {
        ((Foo*) arg)->someMethod();
        return 0;
    }

    void callSomeMethodInOtherThread() {
        pthread_create(thread, attr, someMethodWrap, this);
    }
};

Passing additional parameters needs a bit more effort, but that's the general idea.

Fortunately, std::thread from the next standard makes our life much easier... but that's still in the future.

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

Comments

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.