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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## 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 an element in array such that sum of left array is equal to sum of right array
- Maximum sum subarray having sum less than or equal to given sum using Set
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Find Sum of all unique sub-array sum for a given array.
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Maximum sum subarray having sum less than or equal to given sum
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Maximum subarray sum in O(n) using prefix sum
- Construct sum-array with sum of elements in given range
- Minimum array size after repeated replacement of even sum pair with sum
- Sum of XOR of sum of all pairs in an array
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements

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.