📜  使用pthread的超大型数组中的最大元素

📅  最后修改于: 2021-05-25 19:21:17             🧑  作者: Mango

给定非常大的整数数组,请使用多线程在数组中找到最大值。

例子:

Input :  1, 5, 7, 10, 12, 14, 15, 18, 20, 
         22, 25, 27, 30, 64, 110, 220
Output :Maximum Element is : 220

Input :  10, 50, 70, 100, 120, 140, 150, 180, 
         200, 220, 250, 270, 300, 640, 110, 220
Output : Maximum Element is : 640

先决条件:多线程

注意:在MB / GB大小的大型文件中很有用。

如何运行:
它只能在linux环境下运行。
命令 :

>> gcc -pthread maximum.c 
>> ./a.out
// C++ code to find maximum of an array using Multithreading
#include 
#include 
#include 
  
// Size of array
#define max 16
  
// Max number of thread
#define Th_max 4
  
// Array
int a[max] = { 1, 5, 7, 10, 12, 14, 15, 18, 20,
               22, 25, 27, 300, 64, 110, 220 };
  
// Array to store max of threads
int max_num[Th_max] = { 0 };
int thread_no = 0;
  
// Function to find maximum
void maximum(void* arg)
{
    int i, num = thread_no++;
    int maxs = 0;
  
    for (i = num * (max / 4); i < (num + 1) * (max / 4); i++) {
        if (a[i] > maxs)
            maxs = a[i];
    }
  
    max_num[num] = maxs;
}
  
// Driver code
int main()
{
    int maxs = 0;
    int i;
    pthread_t threads[Th_max];
  
    // creating 4 threads
    for (i = 0; i < Th_max; i++)
        pthread_create(&threads[i], NULL,
                       maximum, (void*)NULL);
  
    // joining 4 threads i.e. waiting for
    // all 4 threads to complete
    for (i = 0; i < Th_max; i++)
        pthread_join(threads[i], NULL);
  
    // Finding max element in an array
    // by individual threads
    for (i = 0; i < Th_max; i++) {
        if (max_num[i] > maxs)
            maxs = max_num[i];
    }
  
    printf("Maximum Element is : %d", maxs);
  
    return 0;
}

输出:

Maximum Element is : 300

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。