0
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct ags{
long int **mat1;
long int **mat2;
int *semafor;
int rowsDone;
};
void *thread_multiply(void*);
int main(int args, char** argv){
int mat1x;
int mat1y;
int mat2x;
int mat2y;
int threadsAmount;
printf("Podaj szerokosc pierwszej macierzy: ");
scanf("%i", &mat1x);
printf("Podaj wysokosc pierwszej macierzy: ");
scanf("%i", &mat1y);
printf("Podaj szerokosc drugiej macierzy: ");
scanf("%i", &mat2x);
printf("Podaj wysokosc drugiej macierzy: ");
scanf("%i", &mat2y);
printf("Podaj ilosc watkow: ");
scanf("%i", &threadsAmount);
if(mat1x != mat2y){
printf("Musisz podac odpowiednie macierze!");
return;
}
struct ags* strAgs;
int i;
int j;
for(i = 0; i < mat1y; i++){
for(j = 0; j < mat1x; j++){
strAgs->mat1[i][j] = random();
}
}
for(i = 0; i < mat2y; i++){
for(j = 0; j < mat2x; j++){
strAgs->mat2[i][j] = random();
}
}
for(j = 0; j < mat2x; j++){
strAgs->semafor[j] = 0;
}
strAgs->rowsDone = mat2x;
pthread_t threadsArray[threadsAmount];
for(i = 0; i < threadsAmount; i++){
if(pthread_create(&threadsArray[i], NULL, thread_multiply, (void*) strAgs)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
}
for(i = 0; i < threadsAmount; i++){
if(pthread_join(threadsArray[i], NULL)) {
fprintf(stderr, "Error joining thread\n");
return 1;
}
}
}
void *thread_multiply(void* ptr) {
struct ags* agsStruct = (struct ags*) ptr;
while(agsStruct->rowsDone > 0){
printf("woho\n");
agsStruct->rowsDone--;
}
};
好的,这里是代码。现在是问题。我试图让一个程序在线程中乘以矩阵。试图运行它时,问题是现在出现错误。它编译得很好,只是当我在开始输入5个整数时它刚刚崩溃,我找不到原因。任何想法如何修复它?如何在C程序中查找乘法矩阵的错误
啊。但我需要它的大小mat1x mat1y等等。可能吗? – 2014-11-21 22:42:55
@ user3552292是的。我稍微看一下。 – DevNull 2014-11-21 22:50:38
太棒了,但我试过了。仍然错误。程序甚至不会在struct之后打印一个printf。 – 2014-11-21 22:57:42