基本上,我在main中创建一个包含10个元素的数组,并创建两个线程。第一个线程calcOddIndex将计算数组中具有奇数索引的所有元素的乘积。第二个线程calcEvenIndex将计算数组中具有偶数索引的所有元素的乘积。 Main将在两个子线程完成后最终输出结果。
它给出了以下错误。我想我没有正确地将数组传递给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 :(得分:0)
你需要将你的arr作为(void *)传递:
proto:
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);
}
和
void *calcEvenIndex(void* ptr) {
int* arr = (int*) ptr;
for (int i=0; i<10; i=i+2) {
evenIndexProduct *= arr[i];
}
pthread_exit(0);
}