task 1 2 simple threadsprogramming with proper synchronization modify your program b 4945104
Task 1.2: Simple ThreadsProgramming with Proper Synchronization- Modify your program by introducingpthread mutex variables, so that accesses to the shared variableare properly synchronized. Try your synchronized version with thecommand line parameter set to 1, 2, 5, 10, 100 and 200. Accesses to the shared variables areproperly synchronized if (i) each iteration of the loop inSimpleThread() increments the variable by exactly one and (ii) eachthread sees the same final value. It is necessary to use a pthreadbarrier [2] in order to allow all threads to wait for the last toexit the loop. You must surround all of yoursynchronization-related changes with preprocessor commands, so thatI can easily compile (with the gcc preprocessor option
–Dmacro[=defn]) and get the version of yourprogram developed in Task 1.1. E.g., for(num = 0; num
#endif val = SharedVariable; printf(“*** thread %d sees value%dn”, which, val); SharedVariable = val + 1; ……. } One acceptable output of yourprogram is (assuming 4threads): *** thread 0 sees value 0 *** thread 0 sees value 1
*** thread 0 sees value 2
*** thread 0 sees value 3
*** thread 0 sees value 4
*** thread 1 sees value 5
*** thread 1 sees value 6
*** thread 1 sees value 7
*** thread 1 sees value 8
*** thread 1 sees value 9
*** thread 2 sees value 10
*** thread 2 sees value 11
*** thread 2 sees value 12
*** thread 3 sees value 13
*** thread 3 sees value 14
*** thread 3 sees value 15 *** thread 3 sees value 16
*** thread 3 sees value 17
*** thread 2 sees value 18
*** thread 2 sees value 19
…… Thread 0 sees final value 80
Thread 2 sees final value 80
Thread 1 sees final value 80
Thread 3 sees final value 80 . . .