How to fix no matching function for call to 'pthread_create' in multithreading?

Discussion in 'Programmer's Corner' started by asilvester635, Oct 9, 2017.

  1. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    Basically, I create an array with 10 elements inside main and create two threads. The first thread, calcOddIndex, will calculate the product of all elements with odd index inside the array. The second thread, calcEvenIndex, will calculate the product of all elements with even index inside the array. Main will finally output the results after two child threads finish.

    It gives me the error below. I think I am not passing the array correctly to the pthread_create. How should I fix this?
    chegg.png

    Here is my code:
    Code (Text):
    1.  
    2. #include <pthread.h>
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5.  
    6. /* first thread will calculate the product of all elements with odd index inside the array */
    7. void *calcOddIndex(int arr[10]);
    8. int oddIndexProduct = 1;
    9. /* the second thread will calculate the product of all elements with even index inside the array */
    10. void *calcEvenIndex(int arr[10]);
    11. int evenIndexProduct = 1;
    12.  
    13. int main() {
    14.     pthread_t tidOdd; /* the thread identifier for calcOddIndex */
    15.     pthread_t tidEven; /* the thread identifier for calcEvenIndex */
    16.     pthread_attr_t attr; /* set of thread attributes */
    17.  
    18.     /* create an array with at least 10 elements */
    19.     int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    20.  
    21.     /* get the default attributes */
    22.     pthread_attr_init(&attr);
    23.  
    24.     /* create thread for calcOddIndex */
    25.     pthread_create(&tidOdd, &attr, calcOddIndex, &array);
    26.     /* create thread for calcEvenIndex */
    27.     pthread_create(&tidEven, &attr, calcEvenIndex, &array);
    28.  
    29.     /* wait until both threads is done with their work */
    30.     pthread_join(tidOdd, NULL);
    31.     pthread_join(tidEven, NULL);
    32.  
    33.     // print the product of all elements with odd index
    34.     printf("product of odd index inside the array = %d\n", oddIndexProduct);
    35.     // print the product of all elements with even index
    36.     printf("product of even index inside the array = %d\n", evenIndexProduct);
    37.  
    38. } // end of main
    39.  
    40. /*
    41. calculate the product of all elements with odd index inside the array
    42. */
    43. void *calcOddIndex(int arr[10]) {
    44.     for (int i=1; i<10; i=i+2) {
    45.         oddIndexProduct *= arr[i];
    46.     }
    47.     pthread_exit(0);
    48. } // end of calcOddIndex
    49.  
    50. /*
    51. calculate the product of all elements with even index inside the array
    52. */
    53. void *calcEvenIndex(int arr[10]) {
    54.     for (int i=0; i<10; i=i+2) {
    55.         evenIndexProduct *= arr[i];
    56.     }
    57.     pthread_exit(0);
    58. } // end of calcEvenIndex
    59.  
     
  2. xox

    Member

    Sep 8, 2017
    307
    63
    The callback function you're passing to pthread_create has the wrong prototype ("signature"). It has to be declared like this:

    Code (Text):
    1.  
    2. void* calcOddIndex(void* data);
    3.  
    Inside the function you cast the void* parameter "data" back to the correct type. In other words:

    Code (Text):
    1.  
    2. void* calcOddIndex(void* data){
    3.     int* arr = (int*)data;
    4.     for (int i=1; i<10; i=i+2) {
    5.         oddIndexProduct *= arr[i];
    6.     }
    7.     pthread_exit(0);
    8. } // end of calcOddIndex
    9.  
     
    Last edited: Oct 9, 2017
Loading...