Thread: invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’ when using pthread in C++

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’ when using pthread in C++

    Hi,
    I am using pthread in my main.cpp file and got error in the following code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
    pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t  condition_cond  = PTHREAD_COND_INITIALIZER;
    
    void *functionCount1();
    void *functionCount2();
    int  count = 0;
    #define COUNT_DONE  10
    #define COUNT_HALT1  3
    #define COUNT_HALT2  6
    
    int main()
    {
       pthread_t thread1, thread2;
    
       pthread_create( &thread1, NULL, &functionCount1, NULL);
       pthread_create( &thread2, NULL, &functionCount2, NULL);
       pthread_join( thread1, NULL);
       pthread_join( thread2, NULL);
    
       return 0;
    }
    
    void *functionCount1()
    {
       for(;;)
       {
          pthread_mutex_lock( &condition_mutex );
          while( count >= COUNT_HALT1 && count <= COUNT_HALT2 )
          {
             pthread_cond_wait( &condition_cond, &condition_mutex );
          }
          pthread_mutex_unlock( &condition_mutex );
    
          pthread_mutex_lock( &count_mutex );
          count++;
          printf("Counter value functionCount1: %d\n",count);
          pthread_mutex_unlock( &count_mutex );
    
          if(count >= COUNT_DONE) return(NULL);
        }
    }
    
    void *functionCount2()
    {
        for(;;)
        {
           pthread_mutex_lock( &condition_mutex );
           if( count < COUNT_HALT1 || count > COUNT_HALT2 )
           {
              pthread_cond_signal( &condition_cond );
           }
           pthread_mutex_unlock( &condition_mutex );
    
           pthread_mutex_lock( &count_mutex );
           count++;
           printf("Counter value functionCount2: %d\n",count);
           pthread_mutex_unlock( &count_mutex );
    
           if(count >= COUNT_DONE) return(NULL);
        }
    
    }
    g++ gives error:
    /home/C/test/main.cpp|96|error: invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’|
    /home/C/test/main.cpp|96|error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’|
    /home/C/test/main.cpp|97|error: invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’|
    /home/C/test/main.cpp|97|error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’|
    Could you explain a little bit about these mistakes and how to fix it? Thanks in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your thread functions should take a void * parameter.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Your thread functions should take a void * parameter.

    --
    Mats
    And even if you don't need that parameter you still need to declare it. If the compiler whines about "unused parameter" just don't give it a name
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by brewbuck View Post
    just don't give it a name
    could you do it in C?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    could you do it in C?
    I don't think so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Yes, I have tried to add a parameter to the two functions. It's odd that both
    Code:
    void *functionCount1(void * p)
    void *functionCount2(void * p)
    Code:
    void *functionCount1(int * p)
    void *functionCount2(int * p)
    still result in the same error
    /home/C/test/main.cpp|96|error: invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’|
    /home/C/test/main.cpp|96|error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’|
    /home/C/test/main.cpp|97|error: invalid conversion from ‘void* (*)()’ to ‘void* (*)(void*)’|
    /home/C/test/main.cpp|97|error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’|
    Seems like the two functions are always recognized as void* (*)(), instead of taking a parameter.

    By the way, I copied this example from Linux Tutorial: POSIX Threads as my first multi-threading program, where the code is C. However I am going to apply it to my C++ code.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you change your prototypes too?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by brewbuck View Post
    And even if you don't need that parameter you still need to declare it. If the compiler whines about "unused parameter" just don't give it a name
    I believe in C, you can use...
    (void)unused_param;
    ...instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I believe in C, you can use...
    (void)unused_param;
    ...instead.
    Yes - that also works in C++ of course.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Did you change your prototypes too?
    My bad. Now it works! Thanks!

    In case my function takes multiple parameters possibly with different types, how to pass them into pthread_create in the last parameter "void * arg"?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Make a struct or class that you pass along.
    Btw, why is this in the C forum when it is C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    159
    Thanks Elysia!
    Sorry to spam here with C++ stuff. I would be careful next time.

  13. #13
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    and one more programmer has been doomed to eternal suffering... Hopefully he knows what is waiting for him. Synchronization problems, deadlocks, sudden crashes due to wild pointers hitting from other threads, harder debugging, starvations, hard to spot performance issues, pain of asm while writing atomic compare and swap...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. invalid char to char conversion?
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2007, 05:16 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Invalid conversion from 'const void*' to 'void*' error
    By prawntoast in forum C Programming
    Replies: 3
    Last Post: 05-01-2005, 10:30 AM