数组之和是一个小问题,我们必须通过遍历整个数组来添加数组中的每个元素。但是当元素的数量太大时,可能需要很多时间。但这可以通过将数组分成几个部分并同时求出每个部分的和来解决,即并行求出每个部分的和。
null
这可以通过使用 多执行绪 使用处理器的每个核心的地方。在我们的例子中,每个核心将计算一部分的总和,最后我们将把所有部分的总和相加,得到最终的总和。通过这种方式,我们既可以提高程序的性能,也可以利用处理器的内核。
最好每个内核使用一个线程。尽管为了更好地理解多线程,您可以创建任意多个线程。
例如:
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
注—— 建议在基于Linux的系统中执行该程序。 使用以下代码在linux中编译:
g++ -pthread program_name.cpp
代码–
// 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; } |
输出:
sum is 600
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END