Skip to content

Instantly share code, notes, and snippets.

@PiotrWegrzyn
Last active June 6, 2018 11:48
Show Gist options
  • Save PiotrWegrzyn/890bf78f7520d8ba0853711bd9c2e086 to your computer and use it in GitHub Desktop.
Save PiotrWegrzyn/890bf78f7520d8ba0853711bd9c2e086 to your computer and use it in GitHub Desktop.
This program generates a table filled with random numbers in a thread, then counts the sum and avarage of each row in a separate thread and then counts the avarage of sums of all rows in a thread.
//compile with: g++ name.cpp -o name -lpthread
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <assert.h>
using namespace std;
int rows;
int columns;
int ** table;
int * sum;
void *generateRandom(void * n)
{
for(int i =0; i <rows;i++ ){
for(int j =0; j<columns;j++)
table[i][j]=rand() % 20 ;
}
pthread_exit(NULL);
}
void *countSum(void * n)
{ int sum=0;
int row = (int )n;
for(int i =0; i <columns;i++ ){
sum +=table[row][i];
}
cout << "Thread id: "<<row <<"sum of row "<< row << " is "<< sum<<endl;
pthread_exit((void*)sum);
}
void *getAvarage(void *n){
int sumOfSums=0;
for(int i =0 ; i< rows; i ++){
sumOfSums +=sum[i];
}
pthread_exit((void*)sumOfSums);
}
int main (int argc, char *argv[])
{
srand (time(NULL));
cout << "\nThis program generates a table filled with random numbers in a thread, then counts the sum and avarage of each row in a separate thread and then counts the avarage of sums of all rows in a thread."<<endl;
cout <<endl<<"Please input the number of rows of the column:";
cin>>rows;
cout <<"Please input the number of columns of the column:";
cin >> columns;
sum = new int[rows];
table= new int*[rows];
for(int i =0 ;i < rows; i ++){
table[i]= new int[columns];
}
pthread_t thread[rows+2];
pthread_attr_t attr;
long t;
void *result;
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
//creating a thread that generates random numbers in the table
pthread_create(&thread[0], &attr, generateRandom,(void*)t);
cout<<endl << "Creating a thread with id 0. Generating random numbers.."<<endl;
//waiting for the thread to end
pthread_join(thread[0], &result);
cout << "Random number generation DONE. Table contains:"<<endl;
//printing the generated table
for(int i =0 ; i <rows; i ++){
cout << "| ";
for(int j = 0 ; j < columns;j++){
cout << table[i][j]<<" | ";
}
cout << endl;
}
//creating threads with id from 1 to "rows".
for(t=0; t<rows; t++) {
cout <<"Main: creating thread "<< t+1<<endl;
pthread_create(&thread[t+1], &attr, countSum, (void*)t);
}
//Joining threads (waiting for them to finish)
for(t=0; t<rows; t++) {
pthread_join(thread[t+1], &result);
sum[t]= (int)result;
cout << "Main: completed join with thread "<<t+1<<"."<<endl;
}
pthread_create(&thread[rows+1], &attr, getAvarage,(void*)t);
cout <<endl<<"Creating a thread with id "<<rows+1<<". \nComputing the sum of ALL threads.."<<endl;
//waiting for the thread to end
pthread_join(thread[rows+1], &result);
printf("Main: completed join with thread %ld.\n\nThe sum of ALL threads is %ld\n",rows+1,(int)result);
//computing the avarage in all rows
float avarageOfAllRows=0;
for(int i = 0; i < rows; i ++){
avarageOfAllRows +=sum[i];
}
avarageOfAllRows/=rows;
cout << "The avarage of ALL threads is "<< avarageOfAllRows<<endl;
pthread_attr_destroy(&attr);
printf("Main: program completed with no errors. All threads finished. Exiting.\n");
pthread_exit(NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment