Modify the included program to implement the strict alternationsolution to achieve mutual exclusion. It does not matter whetherThread 0 goes first or Thread 1, but it is important that thethread strictly alternate.
PROGRAM:
#include
#include
#include
int count;
int turn = 0; // Shared variable used to implement strictalternation
void* myFunction(void* arg)
{
  int actual_arg = *((int*) arg);
 Â
  for(unsigned int i = 0; i < 10; ++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
 Â
while(turn != actual_arg);
 Â
 Â
 Â
// Beginning of the critical region
 Â
count++;
std::cout << \"Thread #\" << actual_arg << \" count= \" << count << std::endl;
 Â
// 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.
 Â
 Â
// Random wait - This code is just to ensure that the threads
// show data sharing problems
int max = rand() % 100000;
 Â
for (int x = 0; x < max; x++);
 Â
// End of random wait code
 Â
  }
 Â
  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 < 2; ++i) {
args[i] = i;
rc[i] = pthread_create(&ids[i], NULL, myFunction, (void*)&args[i]);
}
 Â
for(unsigned int i = 0; i < 2; ++i) {
pthread_join(ids[i], NULL);
}
 Â
std::cout << \"Final count = \" << count <pthread_exit(NULL);
}