There are multiple threads each with the same format, running different actions eg. thread2 runs actions21-28 etc.
void *Thread1(void *threadid) {
action11();
action12();
action13();
action14();
action15();
action16();
action17();
while (!((gS == 7))) {};
action18();
pthread_exit(NULL);
}
Each action then looks like this:
void action11() {
int loc1;
loc1 = 8;
gK = loc1;
}
gK being a global variable declared elsewhere as 0. Other actions are almost the exact same calling different global variables, however some actions would be slightly different such as these critical actions:
void action13() {
int loc1;
loc1 = 8;
gH = (gH + loc1);
}
void action16() {
int loc1;
loc1 = 2;
gV = (gL * loc1);
int loc2;
loc2 = 10;
gL = (gV + loc2);
}
these actions are calling other global variables modified by other actions. The program is supposed to run concurrently, however there are bugs with modifying values too early or too late so I need to use mutexes within these thread functions to fix these, but I dont understand where to place them. I think the end result should have the global variables all be the same value on each run, eg.
start: gK = 0, gZ = 0, gP = 0, etc
end: gK = x, gZ = y, gP = z etc
start: gK = 0, gZ = 0, gP = 0, etc
end: gK = x, gZ = y, gP = z etc
I tried locking and unlocking mutexes at the start and end of each thread but this just makes it a sequential program not concurrent. I also tried locking before and after each critical action is called, but the final values are still different every run eg
start: gK = 0, gZ = 0, gP = 0, etc
end: gK = x, gZ = y, gP = z etc
start: gK = 0, gZ = 0, gP = 0, etc
end: gK = i, gZ = j, gP = k etc