-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixthread.c
More file actions
63 lines (57 loc) · 1.13 KB
/
matrixthread.c
File metadata and controls
63 lines (57 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
int matA[4][4];
int matB[4][4];
int matC[4][4];
int step=0;
void* multi(void* arg)
{
int c=step++;
int *n= (int*)arg;
int x= *n;
for (int i = c; i < (c + 1) ; i++)
for (int j = 0; j < x; j++)
for (int k = 0; k < x; k++)
matC[i][j] += matA[i][k] * matB[k][j];
}
int main()
{
int x;
printf("Input the side of the matrix : \n");
scanf("%d",&x);
printf("Input the first matrix: \n");
for(int i=0;i<x;i++)
{
for(int j=0;j<x;j++)
{
scanf("%d",&matA[i][j]);
}
}
printf("Input the second matrix: \n");
for(int i=0;i<x;i++)
{
for(int j=0;j<x;j++)
{
scanf("%d",&matB[i][j]);
}
}
pthread_t t[x];
for(int i=0;i<x;i++)
{
pthread_create(&t[i],NULL,multi,&x);
}
for(int i=0;i<x;i++)
{
pthread_join(t[i],NULL);
}
for(int i=0;i<x;i++)
{
for(int j=0;j<x;j++)
{
printf("%d\t\n",matC[i][j]);
}
printf("\n");
}
}