-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacierz.c
97 lines (72 loc) · 1.87 KB
/
macierz.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "threadpool.h"
#include <unistd.h>
#define POOL_SIZE 4
pthread_cond_t master;
pthread_mutex_t lock;
int done;
int *mat;
int d;
void fun(void* v, size_t argsz) {
if (argsz < sizeof(int) * 2)
return;
int *data = (int*)v;
int i = data[0];
int val = data[1];
int t = data[2];
// Sleep for t miliseconds
usleep(t);
pthread_mutex_lock(&lock);
//Get mutex
mat[i] = val;
done++;
if (done == d)
pthread_cond_signal(&master);
pthread_mutex_unlock(&lock);
// Release mutex
// Maybe wake up master
}
int main() {
done = 0;
int err;
if ((err = pthread_mutex_init(&lock, 0) != 0))
return err;
if ((err = pthread_cond_init(&master, 0)) != 0)
return err;
pthread_mutex_lock(&lock);
thread_pool_t *pool = (thread_pool_t *) malloc(sizeof(thread_pool_t));
thread_pool_init(pool, POOL_SIZE);
int n, m;
scanf("%d\n%d", &n, &m);
d = n * m;
mat = (int*) malloc(sizeof(int) * d);
runnable_t *runs = (runnable_t *) malloc(sizeof(runnable_t) * d);
for (int i = 0; i < d; i++) {
int v, t;
scanf("%d %d", &v, &t);
runs[i].function = &fun;
runs[i].arg = malloc(sizeof(int) * 3);
((int*)runs[i].arg)[0] = i;
((int*)runs[i].arg)[1] = v;
((int*)runs[i].arg)[2] = t;
runs[i].argsz = sizeof(int) * 3;
defer(pool, runs[i]);
}
while (done != d)
pthread_cond_wait(&master, &lock);
if ((err = pthread_mutex_unlock(&lock) != 0))
return err;
long long out = 0;
for (int i = 0; i < d; i++) {
out += mat[i];
if ((i + 1) % m == 0) {
printf("%lld\n", out);
out = 0;
}
}
thread_pool_destroy(pool);
for (int i = 0; i < d; i++)
free(runs[i].arg);
free((void*) runs);
free(pool);
return 0;
}