【问题标题】:how can i pass a variable 2d array as a parameter to a thread function如何将变量二维数组作为参数传递给线程函数
【发布时间】:2021-08-17 11:25:21
【问题描述】:

我想用用户给定的尺寸创建一个 2d 并创建一些线程来执行以下操作。我想找到每一行的最大值并将其保存到一维数组中,以便稍后我可以创建一个二维数组,其中包含每个元素与同一行最大值的距离(例如 d[i][j]= max [i]-a[i][j];)。我面临的问题是我无法将二维数组传递给线程函数,当我编译它时,它总是显示订阅既不是数组也不是向量。 我已经尝试了以下

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
int k=0;
int counter=1;
int n = 0;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;   //mutex initializer


void *max_dist(void *arguments){                    // function with an array as parameter every thread executes where the first thread summs the numbers form 1 to k  
   int i,j;                                        // and the other threads do the same from (((counter*k)-k)+1) to (counter*k) in both
   int **args_array = (int *)arguments;           // cases the counter increases and lastly that local sum is added to the totall sum 
   int *max;
   int **d = (int **)malloc(n*sizeof(int *));   //creating a 2d array to store the distance of every element from its' max
   for(i=1; i<=n; i++){
        d[i] = (int *)malloc(n*sizeof(int));
    }
   pthread_mutex_lock(&mut);                 // all these actions are done within the critical segment of the function code which is protected  
   max = (int *)malloc(n*sizeof(int));
   int max_tmp;
       if(counter==1){                    //between the lock and unlock and finally the threads are terminated with pthread_exit(NULL)
         for(i=1; i<=k; i++){
             max_tmp=0;
             for(j=1; j<=n; j++){
                 if(args_array[i][j]>max_tmp){
                     max_tmp=args_array[i][j];
                     max[i]=args_array[i][j];
                 }
             }
         }
         counter++;
        }
        else{
            for(i=(((counter*k)-k)+1); i<=(counter*k); i++){
                max_tmp=0;
                for(j=1; j<=n; j++){
                   if(args_array[i][j]>max_tmp){
                     max_tmp=args_array[i][j];
                     max[i]=args_array[i][j];
                   }
                }
            }
            counter++;
        }
        for(i=1; i<=n; i++){
            for(j=1; j<=n; j++){
                d[i][j] = max[i]-args_array[i][j];         //finding the distance of every element from the max of the same line
            }
        }
   pthread_mutex_unlock(&mut);
   pthread_exit(NULL);
}



int main(){
    int i,err1,err2,j;
    int p;
    pthread_t *mythreads;
    printf("Give the array dimension :");
    scanf("%d",&n);
    printf("Give the number of threads :");
    scanf("%d",&p);
    if(n%p!=0){
        printf("The number of threads should be multiple of number of vector elements give again\n");
        scanf("%d",&n);
        scanf("%d",&p);
    }
    mythreads=(pthread_t *)malloc(p*sizeof(pthread_t));   // dynamicly allocates memory for the two arrays
    int **a = (int **)malloc(n*sizeof(int *));            //dynamicly allocates memory for a 2d array of size n*n
    for(i=1; i<=n; i++){
        a[i] = (int *)malloc(n*sizeof(int));
    }
    if (mythreads==NULL || a==NULL){
        printf("Memory not allocated. \n");
        exit(0);
    }
    k=(n/p);
    printf("Give vector elements :"); 
    for(i=1; i<=n; i++){
        for(j=1; j<=n; j++){
        scanf("%d",&a[i][j]);
        }
    }
    for (i=1; i<=p; i++){
        err1=pthread_create(&mythreads[i],NULL,max_dist,(void *)**a);   //creates threads and checks if they are successfully created
        if (err1 != 0){
            printf("Failed to create thread %d\n",i);
            exit(0);
        }
    }
    for (i=1; i<=p; i++){
        err2=pthread_join(mythreads[i],NULL);                     //tells the threads to wait for the others to finish and checks if the waited successfully
        if (err2!=0){
            printf("Failed to join thread %d\n",i);
            exit(0);
        }
    }
    free(mythreads);
    free(a);
    pthread_exit(NULL);
}   

【问题讨论】:

  • 遍历数组时必须从 0-index 开始
  • 顺便说一句。如果所有计算繁重的代码都在互斥锁下执行,那么线程的目的是什么?
  • @tstanisl 你能举个例子吗?一些英语术语让我无法理解

标签: arrays c pthreads


【解决方案1】:

tstanisl 已经指出了程序中的一个主要错误。另一个是

中的双重错误解引用
        err1=pthread_create(&mythreads[i],NULL,max_dist,(void *)**a);   //creates threads

- 因为您想传递整个一维指针数组(实际上是a 的地址,而不是二维数组),所以这样做:

        err1 = pthread_create(&mythreads[i], NULL, max_dist, a);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 2022-08-09
    • 1970-01-01
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多