# Sum of an array using pthreads

Sum of array is a small problem where we have to add each element in the array by traversing through the entire array. But when the number of elements are too large, it could take a lot of time. But this could solved by dividing the array into parts and finding sum of each part simultaneously i.e. by finding sum of each portion in parallel.

This could be done by using **multi-threading** where each core of the processor is used. In our case, each core will evaluate sum of one portion and finally we will add the sum of all the portion to get the final sum. In this way we could improve the performance of a program as well as utilize the cores of processor.

It is better to use one thread for each core. Although you can create as many thread as you want for better understanding of multi-threading.

Examples:

Input : 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 Output : sum is 600 Input : 10, 50, 70, 100, 120, 140, 150, 180, 200, 220, 250, 270, 300, 640, 110, 220 Output : sum is 3030

**Note –** It is advised to execute the program in Linux based system.

Compile in linux using following code:

g++ -pthread program_name.cpp

Code –

`// CPP Program to find sum of array ` `#include <iostream> ` `#include <pthread.h> ` ` ` `// size of array ` `#define MAX 16 ` ` ` `// maximum number of threads ` `#define MAX_THREAD 4 ` ` ` `using` `namespace` `std; ` ` ` `int` `a[] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220 }; ` `int` `sum[4] = { 0 }; ` `int` `part = 0; ` ` ` `void` `* sum_array(` `void` `* arg) ` `{ ` ` ` ` ` `// Each thread computes sum of 1/4th of array ` ` ` `int` `thread_part = part++; ` ` ` ` ` `for` `(` `int` `i = thread_part * (MAX / 4); i < (thread_part + 1) * (MAX / 4); i++) ` ` ` `sum[thread_part] += a[i]; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `pthread_t threads[MAX_THREAD]; ` ` ` ` ` `// Creating 4 threads ` ` ` `for` `(` `int` `i = 0; i < MAX_THREAD; i++) ` ` ` `pthread_create(&threads[i], NULL, sum_array, (` `void` `*)NULL); ` ` ` ` ` `// joining 4 threads i.e. waiting for all 4 threads to complete ` ` ` `for` `(` `int` `i = 0; i < MAX_THREAD; i++) ` ` ` `pthread_join(threads[i], NULL); ` ` ` ` ` `// adding sum of all 4 parts ` ` ` `int` `total_sum = 0; ` ` ` `for` `(` `int` `i = 0; i < MAX_THREAD; i++) ` ` ` `total_sum += sum[i]; ` ` ` ` ` `cout << ` `"sum is "` `<< total_sum << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

sum is 600

## Recommended Posts:

- Maximum element in a very large array using pthreads
- Addition and Subtraction of Matrix using pthreads
- Odd Even Transposition Sort / Brick Sort using pthreads
- Find original array from encrypted array (An array of sums of other elements)
- Find an element in array such that sum of left array is equal to sum of right array
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Find minimum value to assign all array elements so that array product becomes greater
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Count number of permutation of an Array having no SubArray of size two or more from original Array
- Generate original array from an array that store the counts of greater elements on right
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Print modified array after multiple array range increment operations
- Maximum subarray sum in array formed by repeating the given array k times
- Kth smallest element in the array using constant space when array can't be modified
- Check if it is possible to make array increasing or decreasing by rotating the array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.