错误:从'void*'到'void*(*)(void*)'的无效转换 - pthreads

Aqu*_*irl 9 c++ linux g++ pthreads

anisha@linux-y3pi:~> g++ conditionVarTEST.cpp -Wall

conditionVarTEST.cpp: In function ‘int main()’:
conditionVarTEST.cpp:33:53: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’
conditionVarTEST.cpp:33:53: error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
conditionVarTEST.cpp:34:53: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’
conditionVarTEST.cpp:34:53: error:   initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
Run Code Online (Sandbox Code Playgroud)

第33行是这样的:

pthread_create (&A, NULL, (void *) &functionA, NULL);
Run Code Online (Sandbox Code Playgroud)

宣言functionA是这样的:

void functionA (void*);


它的定义是:

void functionA (void* argA)
{
    while (1)
    {
        pthread_mutex_lock (&mutexA);

        if (count < 0)
        {
            pthread_cond_wait (&conditionVariableA, &mutexA);
        }
        else
        {
            // Do something.
            std :: cout << "\nTime to enjoy!";
        }
        pthread_mutex_unlock (&mutexA);
    }
}
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 16

如果查看手册页,您将看到函数参数是

void *(*start_routine) (void *)
Run Code Online (Sandbox Code Playgroud)

也就是说,指向一个函数的指针,该函数接受一个void *参数并返回void *.

要摆脱错误,请将函数更改为return void *,并在不进行类型转换的情况下传递它.return NULL如果您不关心值,则从线程函数返回可以很简单.