Cannot convert '* void (MyClass :: *) (void *) to void * (*) (void *) into the pthread_create function

I am trying to create a new stream with the CameraManager class, but I have the following error:

cannot convert '* void (CameraManager :: *) (void *) to void * (*) (void *) into the pthread_create function

i defined in cameramanager.h:

public:
void *dequeueLoop(void *ptr);

and in cameramanager.cpp

void CameraManager::startDequeuing(){
dequeuing = true;
dequeueThreadId = pthread_create(&dequeueThread, NULL, &CameraManager::dequeueLoop, NULL);
}

void *CameraManager::dequeueLoop(void *ptr){
while(dequeuing){
    highSpeedCamera->dequeue();
    highSpeedCamera->enqueue();
}

I do not want to declare dequeueLoop as a static function, I also tried to declare dequeueLoop as a function of the friend class as follows, but then it does not have the scope of the highSpeedCamera and 'dequeuing' classes and the compiler also tells me that 'dequeueLoop' was not declared in this area

to make dequeueLoop a friend function that I did:

cameramanager.h

public:
friend void *dequeueLoop(void *ptr);

cameramanager.cpp

void CameraManager::startDequeuing(){
    dequeuing = true;
    dequeueThreadId = pthread_create(&dequeueThread, NULL, &CameraManager::dequeueLoop, NULL);
}
void *dequeueLoop(void *ptr){
    while(dequeuing){
        highSpeedCamera->dequeue();
        highSpeedCamera->enqueue();
    }
}

Where am I doing wrong?

+5
3

dequeueLoop

pthreads, - . , -:

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

, , :

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

, , static. , this. -, , -:

void *dequeueLoopWrapper(void *p)
{
    CameraManager *cameraManager = static_cast<CameraManager*>(p);
    camereraManager->dequeueLoop();
    return nullptr;
}

// ...

void CameraManager::startDequeuing()
{
    dequeuing = true;
    dequeueThreadId = pthread_create(&dequeueThread, NULL, dequeueLoopWrapper, this);
}

:

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

- , . dequeueLoop .

To access class members in a free function, you must pass the function thisas the last argument to pthread_create. Then add a free function by passing its argument to a class pointer.

+2
source

All Articles