-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkostra_psed.c
91 lines (75 loc) · 1.79 KB
/
kostra_psed.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
#include <stdio.h>
#include<unistd.h>
#include<thread>
#include<queue>
#include<mutex>
#include<vector>
#include <iostream>
#include<string.h>
std::vector<std::mutex *> zamky; /* pole zamku promenne velikosti */
char *line;
char *to_cstr(std::string a) {
// prevede retezec v c++ do retezce v "c" (char *)
char *str;
str=(char *) malloc(sizeof(char)*(a.length()+1));
strcpy(str,a.c_str());
return str;
}
char *read_line(int *res) {
std::string line;
char *str;
if (std::getline(std::cin, line)) {
str=to_cstr(line);
*res=1;
return str;
} else {
*res=0;
return NULL;
}
}
void f(int ID) {
/* funkce implementujici thread */
printf("Thread %i started\n",ID);
}
int main() {
/*******************************
* Inicializace threadu a zamku
* *****************************/
int num=10;
int num_zamky=15;
std::vector <std::thread *> threads; /* pole threadu promenne velikosti */
/* vytvorime zamky */
zamky.resize(num_zamky); /* nastavime si velikost pole zamky */
for(int i=0;i<num_zamky;i++){
std::mutex *new_zamek = new std::mutex();
zamky[i]=new_zamek;
}
/* vytvorime thready */
threads.resize(num); /* nastavime si velikost pole threads */
for(int i=0;i<num;i++){
std::thread *new_thread = new std::thread (f,i);
threads[i]=new_thread;
}
/**********************************
* Vlastni vypocet psed
* ********************************/
int res;
line=read_line(&res);
while (res) {
printf("%s\n",line);
free(line); /* uvolnim pamet */
line=read_line(&res);
}
/**********************************
* Uvolneni pameti
* ********************************/
/* provedeme join a uvolnime pamet threads */
for(int i=0;i<num;i++){
(*(threads[i])).join();
delete threads[i];
}
/* uvolnime pamet zamku */
for(int i=0;i<num_zamky;i++){
delete zamky[i];
}
}