简体   繁体   English

带有 pthread_create 问题的“无效转换”

[英]“invalid conversion from” with pthread_create issue

Any comment is appreciated for the compile error below.对于下面的编译错误,感谢您的任何评论。 Although my question is similar to other thread: pthread function from a class , I still haven't been able to solve my problem.虽然我的问题类似于其他线程: pthread function from a class ,但我仍然无法解决我的问题。 I am still not that familliar with pointer, and thread programming in C & C++.我仍然不太熟悉 C 和 C++ 中的指针和线程编程。

Error错误

../src/Main.cpp: In function ‘int main(int, char**)’:
../src/Main.cpp:22: error: invalid conversion from ‘unsigned int* (*)(void*)’ to ‘void* (*)(void*)’
../src/Main.cpp:22: error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
make: *** [src/Main.o] Error 1

Main.cpp主文件

#include <process.h>
#include "ThreadInstance.hpp"
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char** argv)
{
    pthread_mutex_t mutex;
    int ht1;
    pthread_t threadId1;
    pthread_attr_t attr1;

    pthread_mutex_init(&mutex, NULL);
    pthread_attr_init(&attr1);
    pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_DETACHED);

    ht1 = pthread_create(&threadId1,
            &attr1,
            &ThreadInstance::ThreadEntryPoint,
            //(void *)readThread);
            NULL);

    unsigned long rc = 0;
    rc = pthread_join(ht1, NULL);

    return 0;
}

ThreadInstance.hpp线程实例.hpp

#ifndef _SCENE_CLASSIFY_THREAD_H
#define _SCENE_CLASSIFY_THREAD_H

#ifndef STDCALL
#define STDCALL __attribute__((stdcall))
#endif

using namespace std;

class ThreadInstance
{
    public:
        ThreadInstance();
        ThreadInstance(int camNum);

        void startUp();

        static unsigned STDCALL* ThreadEntryPoint(void* pThis)
        {
            //static unsigned __stdcall ThreadEntryPoint(void* pThis) {
            ThreadInstance *ptr = (ThreadInstance*) pThis;
            ptr->startUp();
            //return 1; // Returns error "invalid conversion from ‘int’ to ‘unsigned int*’" when the function is declared as pointer.
                            // Since returning either 1 or 0, 
                            // compile error still occurs. 
                            // So this return value should not be the cause.

            return 0; 
        }
        ~ThreadInstance();
};
#endif

Note: Only necessary part is shown注:仅显示必要部分

Your ThreadEntryPoint must return void* .您的ThreadEntryPoint必须返回void*

The error indicates the type that is expected, and that is the function pointer type that you are required to use.该错误指示预期的类型,即您需要使用的 function 指针类型。

The start function returns void* and takes void* .开始 function 返回void*并采用void* Yours returns unsigned int* .你的返回unsigned int*

Change the method to return a pointer to void.更改方法以返回指向 void 的指针。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM