Modify this code to use multiple threads with the same data 1.Modify the main fu
ID: 3791159 • Letter: M
Question
Modify this code to use multiple threads with the same data
1.Modify the main function to implement a loop that reads 10 integers from the console (user input) and stores these numbers in a one-dimensional (1D) array (this code will go right after the comment that says “Add code to perform any needed initialization or to process user input”). You should use a global array for this.
2.Implement a separate pthread function function for each one of the following operations:
a.Count and print out how many of the entered numbers are negative. This function must be named countNegatives
b.Calculate and print the average value of all the numbers entered. This function must be named average
c.Print the numbers in reverse order from the order in which they were entered. This function must be named reverse
3.Modify the main function to create one pthread for each one of the functions that you implemented in (3) above (this code will go between the comment that says “TODO: Modify according to assignment requirements” and the “if (rc)” check).
Compile your program and run it several times. If the output of your program is garbled, you may need to add a small delay in between creating the next thread.
#include <pthread.h>
#include <iostream>
using namespace std;
void *routineName(void *arg)
{
// TODO: Add code that implements
// the thread's functionality
cout << "Thread is running..." << endl;
return 0;
}
int main()
{
pthread_t id;
int rc;
int ints;
for(int x; x<10; x++)
{
cout << "Enter Integer: " <<endl;
cin >>
}
rc = pthread_create(&id, NULL, routineName, NULL);
if (rc){
cout << "ERROR; return code from pthread_create() is " << rc << endl;
return -1;
}
pthread_exit(0);
}
Explanation / Answer
#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
int ints[10];
//Method to count negatives in the input array
void *countNegatives(void*){
int count = 0;
for(int i=0;i<10;i++){
if(ints[i]<0){
count++;
}
}
cout<<"Number of negatives: "<<count<<endl;
}
//Method to calculate and print average of numbers
void *average(void*){
int sum=0;
for(int i=0;i<10;i++){
sum+=ints[i];
}
cout<<"Average: "<<(float)sum/10<<endl;
}
//Method to print the numbers in reverse order
void *reverse(void*){
cout<<"Printing array in reverse ";
for(int i=9;i>=0;i--){
cout<<ints[i]<<" ";
}
cout<<endl;
}
int main(){
pthread_t t1,t2,t3;
int rc;
for(int x; x<10; x++)
{
cout << "Enter Integer: " <<endl;
cin >> ints[x];
}
//Creating pthread to countNegatives
rc = pthread_create(&t1, NULL, countNegatives, NULL);
if(rc){
cout << "Error:unable to create thread," << rc << endl;
}
//Adding delay
sleep(1);
//Creating pthread to compute average
rc = pthread_create(&t2, NULL, average, NULL);
if(rc){
cout << "Error:unable to create thread," << rc << endl;
}
//Adding delay
sleep(1);
//Creating pthread to print in reverse
rc = pthread_create(&t3, NULL, reverse, NULL);
if(rc){
cout << "Error:unable to create thread," << rc << endl;
}
//Adding delay
sleep(1);
void* status;
//waiting for t1 to join
rc = pthread_join(t1,&status);
if(rc){
cout << "Error:unable to join," << rc << endl;
}
//waiting for t2 to join
rc = pthread_join(t2,&status);
if(rc){
cout << "Error:unable to join," << rc << endl;
}
//waiting for t3 to join
rc = pthread_join(t3,&status);
if(rc){
cout << "Error:unable to join," << rc << endl;
}
pthread_exit(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.