Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

i defined in the cameramanager.h file:

public:
void *dequeueLoop(void *ptr);

and in the 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 don't want to declare dequeueLoop as a static function i also tried to declare dequeueLoop as a class friend function in the following way but then it doesn't have scope on class variables 'highSpeedCamera' and 'dequeuing' and the compiler also tell me that 'dequeueLoop' was not declared in this scope

to make dequeueLoop a friend function 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 i'm doing wrong?

like image 961
Gabriele Gambotto Avatar asked Aug 17 '12 12:08

Gabriele Gambotto


People also ask

What is type pthread_ t?

pthread_t is the data type used to uniquely identify a thread. It is returned by pthread_create() and used by the application in function calls that require a thread identifier. The thread is created running start_routine, with arg as the only argument.

What does pthread_ create return?

pthread_create() returns zero when the call completes successfully. Any other return value indicates that an error occurred. When any of the following conditions are detected, pthread_create() fails and returns the corresponding value.

How does pthread_ create work?

The pthread_create() function is used to create a new thread, with attributes specified by attr, within a process. If attr is NULL, the default attributes are used. If the attributes specified by attr are modified later, the thread's attributes are not affected.


2 Answers

I don't want to declare dequeueLoop as a static function

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);
like image 194
Mike Seymour Avatar answered Oct 06 '22 01:10

Mike Seymour


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<CameraManager*>(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);
}
like image 40
Some programmer dude Avatar answered Oct 05 '22 23:10

Some programmer dude