Question
C++ Code: For the c++ code below: #include #include int count; int turn = 0; // Shared variable used to implement strict alternation void* myFunction(void*
C++ Code:
For the c++ code below:
#include
#include
int count;
int turn = 0; // Shared variable used to implement strict alternation
void* myFunction(void* arg)
{
int actual_arg = *((int*) arg);
for(unsigned int i = 0; i
// TODO:
// Make sure that the thread waits for its turn
// before it enters the critical region.
//
// HINT: The thread ID is stored in actual_arg
// Beginning of the critical region
count++;
std::cout
// End of the critical region
// TODO:
// Make sure that the other thread gets a turn
//
// HINT: There are only two threads: 0 and 1
// You can use the C++ NOT operator (!)
// to toggle back and forth.
}
pthread_exit(NULL);
}
// HINT: It is not necessary to make any changes in main()
int main()
{
int rc[2];
pthread_t ids[2];
int args[2];
count = 0;
for(unsigned int i = 0; i
args[i] = i;
rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]);
}
for(unsigned int i = 0; i
pthread_join(ids[i], NULL);
}
std::cout
pthread_exit(NULL);
}
int count;
int turn = 0; // Shared variable used to implement strict alternation
void* myFunction(void* arg)
{
int actual_arg = *((int*) arg);
for(unsigned int i = 0; i
// TODO:
// Make sure that the thread waits for its turn
// before it enters the critical region.
//
// HINT: The thread ID is stored in actual_arg
// Beginning of the critical region
count++;
std::cout
// End of the critical region
// TODO:
// Make sure that the other thread gets a turn
//
// HINT: There are only two threads: 0 and 1
// You can use the C++ NOT operator (!)
// to toggle back and forth.
}
pthread_exit(NULL);
}
// HINT: It is not necessary to make any changes in main()
int main()
{
int rc[2];
pthread_t ids[2];
int args[2];
count = 0;
for(unsigned int i = 0; i
args[i] = i;
rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*) &args[i]);
}
for(unsigned int i = 0; i
pthread_join(ids[i], NULL);
}
std::cout
pthread_exit(NULL);
}
#include
int count;
void* myFunction(void* arg) { int actual_arg = *((int*) arg); for(unsigned int i = 0; i
int main() { int rc[2]; pthread_t ids[2]; int args[2]; count = 0; for(unsigned int i = 0; i
Execute the pthread join program several times. Examine the output carefully. You should notice a problem in the the comments carefully. Read the Linux manual page that describes the pthread join function [URL: https://linux.die.net/man/3/pthread_join] and make sure that you understand the functionality that pthread_join provides and how to call (invoke) this function. Correct the problem. Look for the // TODO comment and address it (i.e., implement the functionality described in the comment). Hint: The solution requires only one (1) line of code. Build and run your program and make sure that it works correctly
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