Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Multithreading C Language: can someone help fix this code? I am trying to synchronize threads but the number of threads gets reduced by half with

Multithreading C Language: can someone help fix this code? I am trying to synchronize threads but the number of threads gets reduced by half with each iteration. I am currently trying to use a global variable to check if each necessary thread has made it, but I'm still not syncing and some threads are returning to main early. Please help! I have included the code as text and an image:
#include
#include
#include
#include
double* mathStore = NULL;
int n, m //n and m are passed by user and always 2^x = m/n, m is number of threads to create
int count; // count is num of threads at sync point
pthread_t* threads = NULL;
pthread_mutex_t lock;
void *worker (void *arg){
long tid =(long)arg; //Thread ID number is it's index in threads[]
int localM = m;
int cumulativeThreads = localM; //Keeps track of how many threads need to sync
/*Do some math here and store in mathStore[tid]*/
//Increment count to show thread made it past storage
pthread_mutex_lock(&lock);
count++;
pthread_mutex_unlock(&lock);
//While loop to check syncing. Only breaks if count=m / all threads have stored their math
while(1){
pthread_mutex_lock(&lock);
if (count == cumulativeThreads){
break;
}
else {
pthread_mutex_unlock(&lock);//count not met yet break lock so we can check again
}
}
pthread_mutex_unlock(&lock); //count met, lock needs broken so others can check
int half = localM/2; //keeps track of how many indexes are "upper" indexes
int halfPoint = half; //keeps track of half way point between all active threads
cumulativeThreads = cumulativeThreads + half;//new amount of threads that need to sync
while (localM >1){//while loop that runs until one thread is left
if(tid >= halfPoint){
/*if thread index is an "upper" index, find its lower half partner and join
Store the sum of the partners in the index and increment count to show storing is done*/
long partner_tid = tid - half;
pthread_join(threads[partner_tid],NULL);
mathStore[tid]+= mathStore[partner_tid];
pthread_mutex_lock(&lock);
count++;
pthread_mutex_unlock(&lock);
//while to check sync. Breaks when count = m + cumulative number of upper indexed threads
while(1){
pthread_mutex_lock(&lock);
if(count == cumulativeThreads){
break;
}
else {
pthread_mutex_unlock(&lock);
}
}
pthread_mutex_unlock(&lock);
localM = localM/2; //local M keeps track of active threads so we reduce by half
half = localM/2;//Store the num of "upper" threads
halfPoint = halfPoint + half;//Half way point is the index of halfway. Only upper threads remain, so index is above the original half
cumulativeThreads = cumulativeThreads + half; //New num needed to sync
}
}
/*Final thread prints off the mathStore array*/
pthread_exit(NULL);
}
int main (int argc, char**argv){
if (argc =2){
printf("Not enough arguments passed.
");
exit(1);
}
m = atoi(argv[1]);
n = atoi(argv[2]);
threads = calloc(m,sizeof(pthread_t));
partialSums = calloc(m, sizeof(double));
count =0;
pthread_mutex_init(&lock,NULL);
for (int i =0; i m; i++){
pthread_create(&threads[i], NULL, worker, (void*)(long)i);
}
free(threads);
free(partialSums);
return 0;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago