Search code examples
c++compilationpthreads

g++ compiler: error invalid conversion from 'pthread_t {aka long unsigned int}' to 'void* (*)(void*)' [-fpermissive]


I'm trying to compile my program on the command line and I got this error. It was pointing to the pthread_create line in the following code. I have the correct import for pthreads and I am running on Ubuntu so I know that's not the problem. Otherwise, I'm clueless about what's going on.

int main() {
    pthread_t thinker;
    if(pthread_create(&thinker, NULL, thinker, NULL)) {
         perror("ERROR creating thread.");
    }
    pthread_join(thinker, NULL);
    return 0;
}

Solution

  • The signature of thread creation is:

    int pthread_create(pthread_t * thread, 
                           const pthread_attr_t * attr,
                           void * (*start_routine)(void *), 
                           void *arg);
    

    If you see in your code, you are passing thinker as 3rd param which is not compatible with void * (*start_routine)(void *). It should be function pointer. It should be:

    void *callback_function( void *ptr ){}
    
    pthread_create(&thinker, NULL, callback_function, NULL)