-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_threads.cpp
More file actions
41 lines (36 loc) · 938 Bytes
/
client_threads.cpp
File metadata and controls
41 lines (36 loc) · 938 Bytes
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
#include "client_threads.h"
#include <fstream>
#include <iostream>
//SYNCHRO_STDOUT
synchro_stdot::synchro_stdot(){
user = false;
lock = PTHREAD_MUTEX_INITIALIZER;
in_use = PTHREAD_COND_INITIALIZER;
}
synchro_stdot::~synchro_stdot(){
user = false;
pthread_cond_destroy(&in_use);
pthread_mutex_destroy(&lock);
}
//arxh critical section gia printing sto stdout
void synchro_stdot::cs_start(){
pthread_mutex_lock(&lock);
while(user)
pthread_cond_wait(&in_use, &lock);
user =true;
}
//telos critical secton gia priting sto stdout
void synchro_stdot::cs_end(){
user=false;
pthread_mutex_unlock(&lock);
pthread_cond_broadcast(&in_use);
}
//gia na xeirizomai kapoia edge cases https://stackoverflow.com/questions/19140148/c-get-total-file-line-number
int get_lines_ofile(char * filename ){
std::ifstream f(filename);
std::string line;
int i;
for (i = 0; std::getline(f, line); ++i)
;
return i;
}