To understand the concept of threads and the role of synchronization primitives to ensure safe global variable access you are supposed to 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.
To understand the concept of threads and the role of synchronization primitives to ensure safe
global variable access you are supposed to 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.
3. 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 to ensure safe access to the global
variable.
4. Now modify your solution so that it does not require any synchronization primitives. An
idea here is that each thread must first find its local sum first, and later the main ()
function can use these local sums to determine the global sum for the entire array.
5. Which approach do you think would run faster, and why?
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)