Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

im currently trying to create a multithread program where I create 3 threads(bes

ID: 674956 • Letter: I

Question

im currently trying to create a multithread program where I create 3 threads(besides the main thread) and assign each a similar task. The task being to read an array of numbers and calculate the sum as well as the min and max. I know that the pthread_create function needs to take in a pointer to a pthread, attributes, a pointer to the name of a function, and a pointer to a parameter for the function the thread will execute. I'm thinking of making my function take in the array as a parameter and use it for the calculations and such yet all examples of functions to pass into create_pthread show that they're void function and takes in a void parameter(like so)
void *my_function(void *param);
How should I go about writing the function I want each pthread to run?

Explanation / Answer

The below program must be helpful in meeting your needs.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <pthreads.h>

#include <errno.h>

#define arraySize 10

pthread_mutex_t ptmVar=PTHREAD_MUTEX_INITIALIZER;

typedef struct task_data

{

     int sum ;

     int min;

     int max;

} task_data ;

void handle_error (int num, char _mssge)

{

     errno = num;

     perror(_mssge);

     exit(EXIT_FAILURE) ;

}

void* myfunction(void * data)

{

     int i, j;

     int maxTemp, minTemp;

     int sizeOfArray;

     int *mydata = (int *)data;

     struct task_data *t_data;

     t_data =(struct task_data *)malloc(sizeof(struct task_data));

     t_data->sum =0;

     t_data->min = mydata[0];

     t_data->max= mydata[0];

     for(i = 0;i< arraySize;i++)

     {

          pthread_mutex_lock (&ptmVar) ;

          t_data->sum+=mydata[i];

          if(mydata[i]<t_data->min)

              t_data->min = mydata[i];

          if(mydata[i]>t_data->max)

              t_data->max = mydata[i];

          pthread_mutex_unlock(&ptmVar);

     }

     pthread_exit(NULL);

}

int main()

{

     int count, i=0;

     pthread_t tid;

     int arr[arraySize];

     struct task_data *task, *task_values;

     for(i=0;i<arrSize;i++)

     {

          printf("Enter Value of arr[%d]: ", i);

          scanf("%d ", &arr[i]);

     }  

     pthread_create(&tid, NULL, myfunction,(void* )&arr);

     task->sum =0;

     task->min = arr[0];

     task->max = arr[0];

     for(i = 0;i< arraySize;i++)

     {

          pthread_mutex_lock (&ptmVar) ;

          task->sum+=arr[i];

          if(arr[i] < task->min)

          {

              task->min=arr[i];

          }

          if(arr[i] > task->max)

          {

              task->max=arr[i];

          }

          pthread_mutex_unlock(&ptmVar);

     }

     pthread_join(tid,NULL);  

     printf("The sum of array of elements is: &d ",task->sum);

     printf("The minimum value of array of elements is: %d ",task->min);

     printf("The maximum value of array of elements is: %d ", task->max);

    

     return 0;

}