c-如何将变量2d数组作为参数传递给线程函数



我想用用户给定的维度创建一个2d,并创建一些线程来执行以下操作。我想找到每一行的最大值,并将其保存到一个1d数组中,这样我以后就可以创建一个2d数组,其中包含每个元素与同一行最大值之间的距离(例如d[I][j]=max[I]-a[I][j];(。我面临的问题是,我无法将2d数组传递给线程函数,当我编译时,它总是显示子分区既不是arry也不是向量。我试过以下

#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 againn");
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 %dn",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 %dn",i);
exit(0);
}
}
free(mythreads);
free(a);
pthread_exit(NULL);
}   

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

err1=pthread_create(&mythreads[i],NULL,max_dist,(void *)**a);   //creates threads

-由于您想要传递整个一维指针数组(a实际所在的地址,而不是二维数组(,请执行以下操作:

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

相关内容

  • 没有找到相关文章

最新更新