cannot convert '*void(MyClass::*)(void*) to void*(*)(void*) in pthread_create function

后端 未结 3 1681
失恋的感觉
失恋的感觉 2021-01-06 01:34

i\'m trying to create a new thread with a class \"CameraManager\" but i have the following error:

cannot convert \'*void(CameraManager:: * )(void*) to

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-06 02:20

    If you want the function to be a member of the class, it must be static. It's because the thread function will be called directly and will not have a valid this pointer. This can be solved by having a wrapper function, that gets passed the actual object and then calls the proper member function:

    void *dequeueLoopWrapper(void *p)
    {
        CameraManager *cameraManager = static_cast(p);
        camereraManager->dequeueLoop();
        return nullptr;
    }
    
    // ...
    
    void CameraManager::startDequeuing()
    {
        dequeuing = true;
        dequeueThreadId = pthread_create(&dequeueThread, NULL, dequeueLoopWrapper, this);
    }
    

    However, I would recommend you start using the threading support in the new standard library:

    void CameraManager::startDequeuing()
    {
        dequeuing = true;
        myThread = std::thread(&CameraManager::dequeueLoop, this);
    }
    

提交回复
热议问题