且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

不能在pthread_create函数中转换* void(MyClass :: *)(void *)到void *(*)(void *)

更新时间:2023-11-30 23:20:40


我不想声明 dequeueLoop 一个静态函数

如果你想使用pthreads,那么你需要一个静态或非成员函数作为入口点。你可以传递指向你的对象的指针到这个函数,使用它作为非静态成员函数的trampoline:

If you want to use pthreads, then you'll need a static or non-member function for the entry point. You can pass a pointer to your object to this function, using it as a trampoline into the non-static member function:

static void * dequeueEntry(void * self) {
    return static_cast<CameraManager*>(self)->dequeueLoop();
}

dequeueThreadId = pthread_create(
    &dequeueThread, NULL, 
    &CameraManager::dequeueEntry, // <-- pointer to trampoline function
    this);                        // <-- pointer to object for member function

或者,如果你有一个现代编译器,你可以使用标准线程库:

Alternatively, if you have a modern compiler, you could use the standard thread library instead:

std::thread thread(&CameraManager::dequeLoop, this);