Below I have attached my code. The problem I am having is that I do not think it is properly computing the sum. It seems as if its missing a number or something along those lines. The program is supposed to read an input number, n, which must be a multiple of 3, and use threads to compute the sum of the square roots. If anyone could point me in the right direction, that would be great. Thank you.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
static double sum=0;
pthread_mutex_t mutex1;
pthread_mutex_init(mutex1);
void sroot(int r)
{
int k=1;
pthread_mutex_lock(&mutex1);
for(k;k<r;k++)
sum=sum+sqrt(k);
pthread_mutex_unlock(&mutex1);
return 0;
}
int main(int argc, char *argv[])
{
int n;
n=atoi(argv[1]);
int a = n/3;
sroot(a); // Computes sum for 1 to n/3
pthread_t tid, tid1;
pthread_create(&tid1,NULL,sroot,(2*n)/3); // Computes sum for (n/3)+1 to 2n/3
pthread_join(tid1, NULL); // Wait for thread 1 to end
pthread_create(&tid2,NULL,sroot,n); // Computes sum for (2n/3)+1 to n
//pthread_join(tid1, NULL); // Wait for thread 1 to end
pthread_join(tid2, NULL); // Wait for thread 2 to end
printf("%f\n",sum); // Print result
return 0;
}