i writen the program below but i am a noobie in multi threading. I
need to develop a develop a multithreaded M3 program in C or C++ by
using POSIX threads(pthreads) library to statistically analyze a set
of data by computing its minimum value, maximum value and the mean of
the population. any idea on how to add 8 worker threads to compute
the minimum, maximum and mean... my data size is 8000. any idea on
how i set the array to 0-7999. thanx for the help...
#include <stdio.h>
#define DATA_SIZE 8000
int result_max;
int result_min;
/* need long integer to hold so as to prevent overflow(in case) */
long result_sum;
int mark[DATA_SIZE];
/* compute max, min and total sum */
void Compute()
{
int i;
result_sum=mark[0];
result_max=mark[0];
result_min=mark[0];
/* start from index 1 instead of 0 as the
* first element is already assigned to the
* result_sum, max and min */
for (i=1; i<DATA_SIZE; i++){
result_sum = result_sum+mark[i];
if (mark[i] > result_max)
result_max = mark[i];
if (mark[i] < result_min)
result_min = mark[i];
}
}
int main()
{
int i;
int mean;
for (i=0; i<DATA_SIZE; i++)
/* i+1 as starting mark is 1 instead of 0 */
mark[i] = i+1;
Compute();
/* integer value for mean is just good enough :) */
mean = result_sum / DATA_SIZE;
printf("min=%d, max=%d, mean=%d\n", result_min, result_max,
mean);
return 0;
}