开发者

pthreads with objects method

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);
开发者_如何学Python    glutKeyboardFunc(Keyboard);
    glutMainLoop();
    return args;
}

Main

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

THX !


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜