Thread: pthread, passing argument to function error

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    15

    pthread, passing argument to function error

    The program seems to work as intended, but I'm getting this error when compiling:

    Code:
    $ gcc -O2 -Wall bar.c -o bar -lpthread -lX11 -L/usr/include/curl/lib -lcurl
    
    bar.c: In function ‘main’:
    bar.c:257:57: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
                         pthread_create(&myThreads[i], NULL, execFunc, &t[i]);
                                                             ^~~~~~~~
    In file included from bar.c:14:0:
    /usr/include/pthread.h:233:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(struct tData *)’
     extern int pthread_create (pthread_t *__restrict __newthread,
                ^~~~~~~~~~~~~~
    Relevant code:

    Code:
    #include<sys/ioctl.h> #include<arpa/inet.h>  
    #include<unistd.h>     /* close()  */
    #include<linux/if.h>   /* struct ifreq */
    #include<X11/Xlib.h>
    #include<curl/curl.h>
    #include<stdlib.h>
    #include<pthread.h> 
    
    
    ...
    
    
    struct tData {
        const char* cmd;
        char* str;
        char* tmpStr;
        int ready;
        unsigned int scroll;
    };
    
    
    void* execFunc(struct tData* td)
    {
        struct tData* t=(struct tData*) td;
        char data[128];
    
    
        FILE *file = popen(t->cmd, "r");
        fgets(data, 128, file);
    
    
        t->tmpStr    = data;
        t->ready  = 1;
    
    
        pclose(file);
        return NULL;
    }
    
    
    ...
    
    
    int main()
    {
    
    
    
    
        #define nThreads            1
    
    
        pthread_t myThreads[nThreads];
        struct tData t[nThreads];
    
    
        t[0].cmd    = "/home/pi/git/weather";
        t[0].str    = NULL;
        t[0].tmpStr = NULL;
        t[0].ready  = 0;
        t[0].scroll = 0;
    
    
    ...
    
    
        for (i = nThreads; i--;)
        {
            pthread_create(&myThreads[i], NULL, execFunc, &t[i]);
            pthread_detach(myThreads[i]);
        }
    
    
    ...
    
    
    return 0;
    
    
    }
    Any ideas?

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    You are passing a pointer to void *(*)(struct tData *) as the function pointer argument and pthread_create() expects void *(*)(void *).

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    15
    Quote Originally Posted by flp1969 View Post
    You are passing a pointer to void *(*)(struct tData *) as the function pointer argument and pthread_create() expects void *(*)(void *).
    The issue was that I didn't completely understand this and how to deal with it.

    I did get the issue fixed by bumping into this tutorial: POSIX Threads Programming

    Here is the fixed code:

    bar.c
    Code:
    /*
    
      gcc -O3 -Wall bar.c -o bar -lpthread -lX11 -L/usr/include/curl/lib -lcurl
    
    
    */
    
    
    #include<sys/ioctl.h>
    #include<arpa/inet.h>
    #include<unistd.h>     /* close()  */
    #include<linux/if.h>   /* struct ifreq */
    #include<X11/Xlib.h>
    #include<curl/curl.h>
    #include<stdlib.h>
    #include<pthread.h>
    
    
    #define NUM_THREADS     1
    ...
    struct tData {
        const char* cmd;
        char* str;
        char* tmpStr;
        int ready;
        unsigned int scroll;
    };
    struct tData t[NUM_THREADS];
    
    
    void *execFunc(void *td)
    {
        struct tData* t=(struct tData*) td;
        char data[128];
    
    
        FILE *file = popen(t->cmd, "r");
        fgets(data, 128, file);
    
    
        t->tmpStr    = data;
        t->ready  = 1;
    
    
        pclose(file);
        return NULL;
    }
    
    
    ...
    
    
    int main()
    {
    
    
        pthread_t myThreads[NUM_THREADS];
    
    
        t[0].cmd    = "/home/pi/git/weather";
        t[0].str    = NULL;
        t[0].tmpStr = NULL;
        t[0].ready  = 0;
        t[0].scroll = 0;
    
    
    ...
    
    
        for (i = NUM_THREADS; i--;)
        {
            pthread_create(&myThreads[i], NULL, execFunc, &t[i]);
            pthread_detach(myThreads[i]);
        }
    
    
    ...}
    Works beautifully now. Even tested with more than one thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing function as argument
    By NeMewSys in forum C Programming
    Replies: 16
    Last Post: 06-11-2008, 02:03 PM
  2. Passing function as an argument.
    By hyaku_ in forum C Programming
    Replies: 1
    Last Post: 12-11-2005, 04:43 AM
  3. passing a function as an argument
    By angelscars in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2005, 09:53 PM
  4. Passing a function as an argument
    By Xzyx987X in forum C Programming
    Replies: 10
    Last Post: 04-20-2004, 10:32 PM

Tags for this Thread