Question: Consider the following scenario: multiple threads execute function Compute and all of them modify a shared variable SharedCounter (critical section). Add the appropriate code (using
Consider the following scenario: multiple threads execute function Compute and all of them modify a shared variable SharedCounter (critical section). Add the appropriate code (using semaphores or mutex) to make sure that only one thread modifies SharedCounter at the time.
#include#include int SharedCounter = 0; void *Compute(); int main(int argc, char *argv[]) { /* multiple threads are created. All of then execute Compute*/ /* wait for threads to exit */ printf("SharedCounter = %d ", SharedCounter); } void *Compute() { int increment; SharedCounter += increment; pthread_exit(0); }

Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
