简体   繁体   English

无法在pthread_create函数中将'* void(MyClass :: *)(void *)转换为void *(*)(void *)

[英]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: 我正在尝试使用类“CameraManager”创建一个新线程,但我有以下错误:

cannot convert '*void(CameraManager:: * )(void*) to void*( * )(void*) in pthread_create function 无法在pthread_create函数中将'* void(CameraManager :: *)(void *)转换为void *(*)(void *)

i defined in the cameramanager.h file: 我在cameramanager.h文件中定义:

public:
void *dequeueLoop(void *ptr);

and in the cameramanager.cpp 并在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 我不想将dequeueLoop声明为静态函数我也尝试以下列方式将dequeueLoop声明为类友元函数但是它没有类变量'highSpeedCamera'和'dequeuing'的范围,编译器也告诉我我认为'dequeueLoop'未在此范围内声明

to make dequeueLoop a friend function i did: 使dequeueLoop成为朋友的功能我做了:

cameramanager.h cameramanager.h

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

cameramanager.cpp 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? 哪里我做错了?

I don't want to declare dequeueLoop as a static function 我不想将dequeueLoop声明为静态函数

If you want to use pthreads, then you'll need a static or non-member function for the entry point. 如果你想使用pthreads,那么你需要一个入口点的静态或非成员函数。 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);

If you want the function to be a member of the class, it must be static . 如果希望函数成为类的成员,则它必须static It's because the thread function will be called directly and will not have a valid this pointer. 这是因为线程函数将被直接调用,并且没有有效的this指针。 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);
}

You can't use a pointer to member function as a function pointer unless it's static. 除非它是静态的,否则不能将指向成员函数的指针用作函数指针。 You'll have to make dequeueLoop a free function, or write a free function as a wrapper to it. 你必须使dequeueLoop成为一个自由函数,或者将一个自由函数作为它的包装器。

To access the class members in a free function, you should have the function pass it's this pointer as the final argument of pthread_create. 要在自由函数中访问类成员,您应该让函数传递它的this指针作为pthread_create的最后一个参数。 Then have the free function cast it's argument to a pointer to the class. 然后让free函数将它的参数转换为指向该类的指针。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将'void(myClass :: *)()转换为void(*)() - cannot convert ‘void (myClass::*)() to void (*)() 错误C2664:&#39;pthread_create&#39;:无法将参数3从&#39;void *(__ clrcall *)(void *)&#39;转换为&#39;void *(__ cdecl *)(void *) - error C2664: 'pthread_create' : cannot convert parameter 3 from 'void *(__clrcall *)(void *)' to 'void *(__cdecl *)(void *) pthread_create ::无法使用&#39;void *&#39;类型的右值初始化&#39;void *(*)(void *)&#39;类型的参数 - pthread_create :: cannot initialize a parameter of type 'void *(*)(void *)' with an rvalue of type 'void *' 如何将void(myClass :: *)()转换为void(*)() - How to convert void (myClass::*)() to void (*)() 无法将'void *(Network :: *)(void *)'转换为'void *(*)(void *)' - cannot convert ‘void* (Network::*)(void*)’ to ‘void* (*)(void*)’ 为什么我们在pthread_create中将函数参数作为void *传递? - Why do we pass function arguments as void* in pthread_create? 如何将void(__thiscall MyClass :: *)(void *)转换为void(__ cdecl *)(void *)指针 - How to convert void (__thiscall MyClass::* )(void *) to void (__cdecl *)(void *) pointer 无法在pthread中从void *(类名)(void *)类型转换为void *(*)(void *)类型 - cannot convert from type void* (class name)(void*) to type void* (*)(void*) in a pthread 转换 std::function<void ()> 作废 (*)()</void> - Convert std::function<void ()> to void (*)() 将void *转换为std :: function <void()> - Convert void* to std::function<void()>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM