gpt4 book ai didi

c - 此线程代码中在哪里生成段错误?

转载 作者:行者123 更新时间:2023-11-30 18:08:00 25 4
gpt4 key购买 nike

在本作业中,应为每个象限使用不同的线程添加两个矩阵。这是我到目前为止的尝试,它产生了一个我无法识别的段错误。

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define N 4 //matrix dimension

int c[4][4];


int a[4][4]={1 ,3, 4, 5, 3, 4, 5, 6,7, 8, 9, 0, 2, 10, 3, 5};//test matrices

int b[4][4]={3, 4, 5, 6, 11, 7, 9, 3, 1, 1, 4, 5,1, 0, 0, 6};



void *cuadrante(void *argv[1]){

int t,i,j,k;

int opcion;

opcion = atoi(argv[1]); //meant to cast the quadrant number


switch (opcion){

case 1:
k=((N/2)-1); //A11 position

for (i=k;i=0;i--){ //1st quadrant
for (j=k;j=0;j--){

c[i][j]=a[i][j]+b[i][j];
}
}
break;

case 2:

k=((N/2)-1); //next to A11

for (i=k-1;i=0;i--){ //2nd quadrant
for (j=k;j<N;j++){

c[i][j]=a[i][j]+b[i][j];
}
}

break;

case 3:

t=N/2; //

for (i=t;i<N;i++){ //3rd quadrant
for (j=(t-1);j=0;j--){

c[i][j]=a[i][j]+b[i][j];
}
}

break;

case 4:

t=N/2; //position A22
for (i=t;i<N;i++){ //4th quadrant
for (j=t;j<N;j++){

c[i][j]=a[i][j]+b[i][j];
}
}

break;


default:
printf("\n Opcion erronea. Intente de nuevo");

}//fsw




}//end cuadrante






int main(int argc,char * argv[])
{
int rc, t;

pthread_t threads[4];

for(t=0;t<4;t++){

rc = pthread_create(&threads[t], NULL, cuadrante, (void *)argv[1]);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n",rc);
return -1;
}//end if

}//end for

printf("Created %d threads.\n", t);
pthread_exit(NULL);
return 0;


}//end main

最佳答案

您将 argv[1] 作为参数传递给所有 4 个线程;那么它们最终不会通过 switch 语句采取相同的路线吗?

此外,您的函数将其参数声明为 void **,但您将 char * 强制转换为 void * >。这必然会引起问题。您可能想要这个:

void *cuadrante(void *arg){

int t,i,j,k;

int opcion;

opcion = atoi((char *)arg); //meant to cast the quadrant number

...

关于c - 此线程代码中在哪里生成段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4034299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com