-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
76 lines (66 loc) · 1.36 KB
/
matrix.c
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#define REDOVI 6
#define STUPCI 6
int suma = 0;
int disp[REDOVI][STUPCI];
void *PrintHello(void *threadid)
{
long tid;
long i;
int red = 0;
tid = (long)threadid;
for (i = 0; i < REDOVI; i++)
{
red = red + disp[tid][i];
suma += disp[tid][i];
}
printf("Suma %ld. reda je %d\n", tid + 1, red);
pthread_exit(NULL);
}
int main()
{
/*Counter variables for the loop*/
int i, j;
int counter = 0;
for (i = 0; i < REDOVI; i++)
{
for (j = 0; j < STUPCI; j++)
{
disp[i][j] = counter;
counter++;
}
}
//Displaying array elements
printf("prva matrica:\n");
for (i = 0; i < REDOVI; i++)
{
for (j = 0; j < STUPCI; j++)
{
printf("%d ", disp[i][j]);
}
printf("\n");
}
pthread_t threads[REDOVI];
int rc;
long t;
for (t = 0; t < REDOVI; t++)
{
printf("In main: creatingthread%ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc)
{
printf("rc, povratna vrijednost pthread_create nije nula, neka greska...\n");
exit(-1);
}
}
for (t = 0; t < REDOVI; t++)
{
pthread_join(threads[t], NULL);
printf("Join thread %ld\n", t);
}
printf("Suma je %d\n", suma);
pthread_exit(NULL);
return 0;
}