Four threads are started to work together to take turns prin…
Four threads are started to work together to take turns printing out each integer once in sequential order from 1 up to some maximum. Two of the threads are executing printEven() and the other two threads are executing printOdd(). The following code is a partial implementation of the functions the threads are executing and the shared variables between them. int MAX = 1000; int counter = 1; //the next number to be printed //two threads execute this function void* printEven(void* arg) { for (int i = 0; i < MAX; i++) { // COMPLETION BEFORE EVEN PRINT printf("Even: %d\n", counter); counter++; // COMPLETION AFTER EVEN PRINT } return NULL; } //two threads execute this function void* printOdd(void* arg) { for (int i = 0; i < MAX; i++) { // COMPLETION BEFORE ODD PRINT printf("Odd: %d\n", counter); counter++; // COMPLETION AFTER ODD PRINT } return NULL; }
Read Details