Objective: Basically, I create an array with 10 elements inside main and create
ID: 3589073 • Letter: O
Question
Objective: 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. Please fix this so it accomplishes the objective above. Also, explain your revisions by commenting on the code
Here is my code:
g++ -c -Werror main.cc main.cc:24:2: error: no matching function for call to 'pthread_create' pthread_create(&tidodd;, Sattr, calcoddIndex, Sarray); /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, Sarray) /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.ol Error 1Explanation / Answer
Your Given CODE
My Code
void *calcOddIndex(int arr[10]);
This function prototype must be as follows
void *calcOddIndex(void *arr);
-- the argument declaration should be of type void pointer.
This void pointer further needs to be converted into integer pointer inside the function definition.
void *calcEvenIndex(void *arr);
This function prototype must be as follows
void *calcEvenIndex(void *arr);
-- the argument declaration should be of type void pointer.
This void pointer further needs to be converted into integer pointer inside the function definition.
pthread_create(&tidOdd, &attr, calcOddIndex, &array);
pthread_create(&tidOdd, &attr, &calcOddIndex, (void *)&array);
The third argument is user-defined function call, this should be specified with address of the function i.e &calcOddIndex
The fourth argument is the parameters to the third argument function name, it should be called with address of the array along with generic pointer casting i.e
(void *)&array.
pthread_create(&tidEven, &attr, calcEvenIndex, &array);
Same like above explanation
printf("%d %d ", i,a[i]);
I added this statement inside the functions of
void *calcOddIndex(void *arr) &
void *calcEvenIndex(void *arr)
whether these functions are able to access the array indices even/odd respectively.
Finally - the compilation step
g++ -Wall arrthrd.cpp -pthread
-pthread option must be used to over come the following linker errors
/tmp/cctO6BV2.o: In function `main':
arrthrd.cpp:(.text+0x88): undefined reference to `pthread_create'
arrthrd.cpp:(.text+0xa7): undefined reference to `pthread_create'
arrthrd.cpp:(.text+0xbb): undefined reference to `pthread_join'
arrthrd.cpp:(.text+0xcf): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
Given code
#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 ", oddIndexProduct);
// print the product of all elements with even index
printf("product of even index inside the array = %d ", 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
My Code
#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(void *arr);
int oddIndexProduct = 1;
/* the second thread will calculate the product of all elements with even index inside the array */
void *calcEvenIndex(void *arr);
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, (void *)&array);
/* create thread for calcEvenIndex */
pthread_create(&tidEven, &attr, &calcEvenIndex, (void *)&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 ", oddIndexProduct);
// print the product of all elements with even index
printf("product of even index inside the array = %d ", evenIndexProduct);
} // end of main
/* calculate the product of all elements with odd index inside the array */
void *calcOddIndex(void *arr) {
int *a=(int *)arr;
for (int i=1; i<10; i=i+2) {
printf("%d %d ", i,a[i]);
oddIndexProduct *= a[i];
}
pthread_exit(0);
} // end of calcOddIndex
/* calculate the product of all elements with even index inside the array */
void *calcEvenIndex(void *arr) {
int *a=(int*) arr;
for (int i=0; i<10; i=i+2) {
printf("%d -%d ", i, a[i]);
evenIndexProduct *= a[i];
}
pthread_exit(0);
} // end of calcEvenIndex
Your Given CODE
My Code
void *calcOddIndex(int arr[10]);
This function prototype must be as follows
void *calcOddIndex(void *arr);
-- the argument declaration should be of type void pointer.
This void pointer further needs to be converted into integer pointer inside the function definition.
void *calcEvenIndex(void *arr);
This function prototype must be as follows
void *calcEvenIndex(void *arr);
-- the argument declaration should be of type void pointer.
This void pointer further needs to be converted into integer pointer inside the function definition.
pthread_create(&tidOdd, &attr, calcOddIndex, &array);
pthread_create(&tidOdd, &attr, &calcOddIndex, (void *)&array);
The third argument is user-defined function call, this should be specified with address of the function i.e &calcOddIndex
The fourth argument is the parameters to the third argument function name, it should be called with address of the array along with generic pointer casting i.e
(void *)&array.
pthread_create(&tidEven, &attr, calcEvenIndex, &array);
Same like above explanation
printf("%d %d ", i,a[i]);
I added this statement inside the functions of
void *calcOddIndex(void *arr) &
void *calcEvenIndex(void *arr)
whether these functions are able to access the array indices even/odd respectively.
Finally - the compilation step
g++ -Wall arrthrd.cpp -pthread
-pthread option must be used to over come the following linker errors
/tmp/cctO6BV2.o: In function `main':
arrthrd.cpp:(.text+0x88): undefined reference to `pthread_create'
arrthrd.cpp:(.text+0xa7): undefined reference to `pthread_create'
arrthrd.cpp:(.text+0xbb): undefined reference to `pthread_join'
arrthrd.cpp:(.text+0xcf): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.