1

Basically i need to make three threads B,C,D to work simultaneously. Thread B sums up the even indexes in a global array X , C sums up the odd indexes in X, D sums up both results while B and C are still summing. I used two mutexes to do so but its not working properly. In the array X given in the code below the results should be: evenSum = 47,oddSum = 127 ,bothSum = 174. any help is greatly appreciated! Thanks!!

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define SIZE 20

int X[SIZE] = {5,4,5,3,7,9,3,3,1,2,9,0,3,43,3,56,7,3,4,4};

int evenSum = 0;
int oddSum = 0;
int bothSum = 0;


//Initialize two mutex semaphores
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;

void* sum_even_indexes(void* args){
    int i;
    for(i=0 ; i<20 ; i+=2){
        pthread_mutex_lock(&mutex1);
        evenSum+=X[i];
        pthread_mutex_unlock(&mutex1);
    }
    pthread_exit(NULL);
}

void* sum_odd_indexes(void* args){
    int i;
    for(i=1 ; i<20 ; i+=2){
        pthread_mutex_lock(&mutex2);
        oddSum+=X[i];
        pthread_mutex_unlock(&mutex2);
    }
    pthread_exit(NULL);
}

void* sum_of_both(void* args){
    int i;
    for(i=0 ; i<SIZE ; i++){
        pthread_mutex_lock(&mutex1);
        pthread_mutex_lock(&mutex2);

        bothSum += (evenSum+oddSum);
        
        pthread_mutex_unlock(&mutex2);
        pthread_mutex_unlock(&mutex1);
    }
    pthread_exit(NULL);
}

int main(int argc, char const *argv[]){
    pthread_t B,C,D;

    /***
     * Create three threads:
     *  thread B : Sums up the even indexes in the array X
     *  thread C : Sums up the odd indexes in the array X
     *  thread D : Sums up both B and C results 
     *  
     *  Note: 
     *      All threads must work simultaneously
    */
    pthread_create(&B,NULL,sum_even_indexes,NULL);
    pthread_create(&C,NULL,sum_odd_indexes,NULL);
    pthread_create(&D,NULL,sum_of_both,NULL);

    //Wait for all threads to finish
    pthread_join(B,NULL);
    pthread_join(C,NULL);
    pthread_join(D,NULL);

    
    pthread_mutex_destroy(&mutex1);
    pthread_mutex_destroy(&mutex2);


    //Testing Print
    printf("Odds Sum = %d\n",oddSum);
    printf("Evens Sum = %d\n",evenSum);
    printf("Both Sum = %d\n",bothSum);
    return 0;
    
}

1 Answer 1

0

The mutexes will not enforce any ordering between D and B or C.

To make such ordering, you need to run D later (after B and C joined), or make D wait on a condition, which is set, when B an C are done.

mutex1 and mutex2 are actually not needed, if you implement proper waiting. You need no mutexes if you start D later, or you need only one, if you'll use waiting on a condition variable

Also it is not clear why you need a loop in D. You have just a sum of two variables.


For real application, parallel array processing is usually done by partitioning array by ranges, not by modulo. It does not make sense at all for small sizes like 20. You'd prefer threadpool threads to avoid thread startup overhead and control count of threads. And sure for just evenSum+oddSum you wouldn't need a thread.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering. You make a very good point,but the trick in my question is that D,B,C all must work simultaneously in a synchronized way somehow...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.