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