--- In [email protected], Ray Devore <[EMAIL PROTECTED]> wrote:
>
> --- freak_creeper21 <[EMAIL PROTECTED]>
> wrote:
> >
> > #include <stdio.h>
> > #include <pthread.h>
> >
> > #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);
> >
^^^^^^^^
SJ: Incorrect, you have not converted the void* back to int correctly
try: *((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<DATA_SIZE; 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);
^^^^^^^
SJ: Forceful conversion to pointer looks fishy. I would prefer "&i"
I assume, that you are on Linux platform and are using the correct
library during compile time, "lpthread".
Hope it helps.
Saurabh
> > /* 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;
> > }
> >