Related Articles

# Sum of an array using pthreads

• Difficulty Level : Easy
• Last Updated : 05 Jan, 2018

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ``#include `` ` `// 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 = { 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;``}`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up