hi, i wrote the foolowing program... it shows that i got errors. the errors say tat syntax error before numeric constant. anyone knows wat is wrong? below is the program tat i worte...
#include
#include
#define DATA_SIZE 8000
#define NUM_THREADS 8
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(void *i)
{
int startIndex; /* starting index of i-th partition of 1000 size */
int j;
int max;
int min;
long sum;
startIndex = (DATA_SIZE/NUM_THREADS) * ((int)i);
sum = mark[startIndex];
max = mark[startIndex];
min = mark[startIndex];
for (j=startIndex; j < startIndex + (DATA_SIZE/NUM_THREADS); j++)
{
sum = sum + mark[j];
if (mark[j] > max)
max = mark[j];
if (mark[j] < min)
min = mark[j];
}
/* update total sum, max, min */
result_sum = result_sum + sum;
if (max > result_max)
{
result_max = max;
}
if (min < result_min)
{
result_min = min;
}
/* terminate worker thread */
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS]; /* declare array of 8 working threads */
pthread_attr_t attr; /* pthread attribute variable */
int result;
int i;
int mean;
for (i=0; i{
/* i+1 as starting mark is 1 instead of 0 */
mark[i] = i+1;
}
/* update result sum, max, min to first mark */
result_sum = mark[0];
result_max = mark[0];
result_min = mark[0];
/* initialize the attribute variable */
pthread_attr_init(&attr);
/* set the thread as joinable */
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
/* create 8 worker threads */
for(i = 0; i < NUM_THREADS; i++)
{
/* creates i-th thread which will execute compute function */
result = pthread_create(&threads[i], &attr, Compute, (void *)i);
/* if creation of thread failed */
if (result)
{
fprintf(stderr, "Error: pthread_create() failed, thread: %d, return code: %d\n", i, result);
return 1;
}
}
/* Free attribute and wait for the other threads */
pthread_attr_destroy(&attr);
/* wait for all 8 worker threads created to terminate */
for(i = 0; i < NUM_THREADS; i++)
{
/* wait for i-th thread termination */
result = pthread_join(threads[i], NULL);
/* if joining failed */
if (result)
{
fprintf(stderr, "Error; pthread_join() failed, thread: %d, return code: %d\n", i, result);
return 1;
}
}
/* 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);
/* terminate main thread */
pthread_exit(NULL);
return 0;
}
can u pinpoint where the error is? i can't see it
I'm not comfortable with the following lines... Not sure if they are correct:
1)
void *Compute(void *i)
2)
result = pthread_create(&threads[i], &attr, Compute, (void *)i);
for 1)
It seems like i is an integer pointer...
due to
startIndex = (DATA_SIZE/NUM_THREADS) * ((int)i);
try changing to
void *Compute(int *i)
for 2)
Should be
result = pthread_create(&threads[i], &attr, Compute(void *)i);
no comma after Compute
Anyway just to comment, the code does seems a bit messy. What is the purpose of this program?