How to fix invalid conversion error while using pthread_create()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bajajv
    New Member
    • Jun 2007
    • 152

    How to fix invalid conversion error while using pthread_create()?

    Hi,
    My code is -

    Code:
    void func1(void* arg)
    {
      printf("In child thread.\n");
    }
    
    int main()
    {
      int ret;
      pthread_t tid; 
      void (*start_routine) (void*);
      start_routine = &func1;
      ret = pthread_create(&tid, NULL, start_routine, attr);
      printf("In main thread.\n");
      return 0;
    }
    I am getting the error - Invalid conversion from
    void (*) (void*) to void* (*) (void*)
    I did not understand why this extra * is coming into picture?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    I think the function should be
    Code:
    void* func1(void* arg)
    {
      printf("In child thread.\n");
    }
    and 10 should be
    Code:
      void *start_routine (void*);

    Comment

    • bajajv
      New Member
      • Jun 2007
      • 152

      #3
      well no, it is not working.

      if we change line 10, will start_routine be still a function pointer, because it doesn't have that * now inside the braces?

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        you are correct, looking again line 10 should be
        Code:
          void *(*start_routine) (void*);

        Comment

        • bajajv
          New Member
          • Jun 2007
          • 152

          #5
          I did not understood one thing.. why do i need to make my func1 returning void* and the function pointer type as void* (*start_routine ) (void*)?

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            if you look at the specification


            of pthread_create
            Code:
            int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                void *(*start_routine)(void*), void *arg);
            the third parameter is a pointer to a function which takes a single parameter pointer to void and returns a pointer to void as a function result. Hence start_routine must be
            Code:
            void* func1(void* arg)

            Comment

            • bajajv
              New Member
              • Jun 2007
              • 152

              #7
              hmm.. I should have checked that first..
              thanks for the help...

              Comment

              Working...