Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- long arr[10000];
- void *find_first_half(void* arg) {
- double *sin_sum = malloc(sizeof(double));
- double temp;
- int i;
- for(i = 0; i < 5000; i++) {
- temp += sin(arr[i]);
- }
- sin_sum = &temp;
- return (void *) sin_sum;
- }
- void *find_second_half(void* arg) {
- double *sin_sum = malloc(sizeof(double));
- double temp;
- int i;
- for(i = 5000; i < 10000; i++) {
- *sin_sum += sin((double)arr[i]);
- }
- sin_sum = &temp;
- return (void *) sin_sum;
- }
- int main() {
- int i;
- for(i = 0; i < 10000; i++) {
- arr[i] = i;
- }
- pthread_t th[2];
- pthread_create(&th[0], NULL, find_first_half, NULL);
- pthread_create(&th[1], NULL, find_second_half, NULL);
- void *first_half, *second_half;
- pthread_join(th[0], &first_half);
- pthread_join(th[1], &second_half);
- printf("%f\n", *(double*)first_half + *(double *)second_half);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement