2017-10-09 68 views
0

基本上,我創建了一個數組,內有10個元素,並創建兩個線程。第一個線程calcOddIndex將計算數組內所有具有奇數索引的元素的乘積。第二個線程calcEvenIndex將計算數組中所有具有偶數索引的元素的乘積。 Main將在兩個子線程完成後最終輸出結果。如何修復在多線程中調用'pthread_create'的匹配函數?

它給了我下面的錯誤。我想我沒有正確地將數組傳遞給pthread_create。我應該如何解決這個問題?

g++ -c -Werror main.cc 
main.cc:24:2: error: no matching function for call to 'pthread_create' 
     pthread_create(&tidOdd, &attr, calcOddIndex, &array); 
     ^~~~~~~~~~~~~~ 
/usr/include/pthread.h:326:5: note: candidate function not viable: no known 
     conversion from 'void *(int *)' to 'void * _Nullable (* _Nonnull)(void * 
     _Nullable)' for 3rd argument 
int pthread_create(pthread_t _Nullable * _Nonnull __restrict, 
    ^
main.cc:26:2: error: no matching function for call to 'pthread_create' 
     pthread_create(&tidEven, &attr, calcEvenIndex, &array); 
     ^~~~~~~~~~~~~~ 
/usr/include/pthread.h:326:5: note: candidate function not viable: no known 
     conversion from 'void *(int *)' to 'void * _Nullable (* _Nonnull)(void * 
     _Nullable)' for 3rd argument 
int pthread_create(pthread_t _Nullable * _Nonnull __restrict, 
    ^
2 errors generated. 
make: *** [main.o] Error 1 

這裏是我的代碼:

#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 

/* first thread will calculate the product of all elements with odd index inside the array */ 
void *calcOddIndex(int arr[10]); 
int oddIndexProduct = 1; 
/* the second thread will calculate the product of all elements with even index inside the array */ 
void *calcEvenIndex(int arr[10]); 
int evenIndexProduct = 1; 

int main() { 
    pthread_t tidOdd; /* the thread identifier for calcOddIndex */ 
    pthread_t tidEven; /* the thread identifier for calcEvenIndex */ 
    pthread_attr_t attr; /* set of thread attributes */ 

    /* create an array with at least 10 elements */ 
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 

    /* get the default attributes */ 
    pthread_attr_init(&attr); 

    /* create thread for calcOddIndex */ 
    pthread_create(&tidOdd, &attr, calcOddIndex, &array); 
    /* create thread for calcEvenIndex */ 
    pthread_create(&tidEven, &attr, calcEvenIndex, &array); 

    /* wait until both threads is done with their work */ 
    pthread_join(tidOdd, NULL); 
    pthread_join(tidEven, NULL); 

    // print the product of all elements with odd index 
    printf("product of odd index inside the array = %d\n", oddIndexProduct); 
    // print the product of all elements with even index 
    printf("product of even index inside the array = %d\n", evenIndexProduct); 

} // end of main 

/* 
calculate the product of all elements with odd index inside the array 
*/ 
void *calcOddIndex(int arr[10]) { 
    for (int i=1; i<10; i=i+2) { 
     oddIndexProduct *= arr[i]; 
    } 
    pthread_exit(0); 
} // end of calcOddIndex 

/* 
calculate the product of all elements with even index inside the array 
*/ 
void *calcEvenIndex(int arr[10]) { 
    for (int i=0; i<10; i=i+2) { 
     evenIndexProduct *= arr[i]; 
    } 
    pthread_exit(0); 
} // end of calcEvenIndex 

回答

0

你需要通過你的ARR爲(無效*):

原:

void *calcOddIndex(void*); 
void *calcEvenIndex(void*); 

FUNC:

void *calcOddIndex(void* ptr) { 
int* arr = (int*) ptr; 
for (int i=1; i<10; i=i+2) { 
    oddIndexProduct *= arr[i]; 
} 
pthread_exit(0); 
} 

and

void *calcEvenIndex(void* ptr) { 
int* arr = (int*) ptr; 
for (int i=0; i<10; i=i+2) { 
    evenIndexProduct *= arr[i]; 
} 
pthread_exit(0); 
} 
相關問題