Write a program using pthreads, which calculates the sum of elements in a hard-coded integer array in parallel using 4 threads. The program must divide the work between 4 threads which run simultaneously. For simplicity, you can assume that the size of the array is 100. Note that the integer array must be declared as a global data structure. Initially code your solution so that the sum of elements is maintained in a global shared variable. Each thread modifies the same shared variable as it sums up elements from the array. Use a suitable synchronization primitive (mutex) to ensure safe access to the global variable. (A sample code of Mutex is attached for your reference)
Write a
integer array in parallel using 4 threads. The program must divide the work between 4
threads which run simultaneously. For simplicity, you can assume that the size of the
array is 100. Note that the integer array must be declared as a global data structure.
Initially code your solution so that the sum of elements is maintained in a global shared
variable. Each thread modifies the same shared variable as it sums up elements from
the array. Use a suitable synchronization primitive (mutex) to ensure safe access to the
global variable. (A sample code of Mutex is attached for your reference)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images