Answered step by step
Verified Expert Solution
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 Im 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 x mn m is number of threads to create
int count; count is num of threads at sync point
pthreadt threads NULL;
pthreadmutext lock;
void worker void arg
long tid longarg; 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 mathStoretid
Increment count to show thread made it past storage
pthreadmutexlock&lock;
count;
pthreadmutexunlock&lock;
While loop to check syncing. Only breaks if countm all threads have stored their math
while
pthreadmutexlock&lock;
if count cumulativeThreads
break;
else
pthreadmutexunlock&lock;count not met yet break lock so we can check again
pthreadmutexunlock&lock; count met, lock needs broken so others can check
int half localM; 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 while loop that runs until one thread is left
iftid 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 partnertid tid half;
pthreadjointhreadspartnertidNULL;
mathStoretid mathStorepartnertid;
pthreadmutexlock&lock;
count;
pthreadmutexunlock&lock;
while to check sync Breaks when count m cumulative number of upper indexed threads
while
pthreadmutexlock&lock;
ifcount cumulativeThreads
break;
else
pthreadmutexunlock&lock;
pthreadmutexunlock&lock;
localM localM; local M keeps track of active threads so we reduce by half
half localM;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
pthreadexitNULL;
int main int argc, charargv
if argc
printfNot enough arguments passed.
;
exit;
m atoiargv;
n atoiargv;
threads callocmsizeofpthreadt;
partialSums callocm sizeofdouble;
count ;
pthreadmutexinit&lock,NULL;
for int i ; i m; i
pthreadcreate&threadsi NULL, worker, voidlongi;
freethreads;
freepartialSums;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started