error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’ - pthreads

后端 未结 4 621
我在风中等你
我在风中等你 2021-02-14 15:30
anisha@linux-y3pi:~> g++ conditionVarTEST.cpp -Wall

conditionVarTEST.cpp: In function ‘int main()’:
conditionVarTEST.cpp:33:53: error: invalid conversion from ‘void*         


        
相关标签:
4条回答
  • 2021-02-14 15:58

    (void *) &functionA will cast your function pointer functionA which is of type void (*)(void*) to a simple void*. The later can't be converted to the first again, so the compiler reports an error. This is one of the reasons why you shouldn't use C-style casts.

    Use pthread_create (&A, NULL, functionA, NULL); instead.

    Also, the return type of a thread function should be void*, not void. So change void functionA(void*) to void* functionA(void*).

    0 讨论(0)
  • 2021-02-14 15:59

    If you look at the manual page you will see that the function argument is

    void *(*start_routine) (void *)
    

    That is, a pointer to a function which takes one void * argument and returns void *.

    To get rid of your errors, change your function to return void *, and pass it without type-casting it. The return from the thread function can be a simple return NULL if you don't care about the value.

    0 讨论(0)
  • 2021-02-14 16:03

    Use

    pthread_create (&A, NULL, functionA, NULL); 
    

    instead of casting.

    Also the function you use to pass to pthread_create should return a void* so to avoid any problems later, consider changing the function signature to accomodate this.

    0 讨论(0)
  • 2021-02-14 16:07

    And as you are using a C++ compiler, you should use a function with C binding, as pthread_create expects a C function:

    extern "C" void* functionA (void*);
    

    C++ and C may have the same calling conventions on your current platform, but there is no guaranty, that this will be the case on other platforms or will be so in the future.

    0 讨论(0)
提交回复
热议问题