-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathexer07a-data-server.cpp
More file actions
103 lines (74 loc) · 2.22 KB
/
Copy pathexer07a-data-server.cpp
File metadata and controls
103 lines (74 loc) · 2.22 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
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
98
99
100
101
102
103
/*
THE DATA SERVER PROBLEM
Version A: Solving the problem using a condition variable
*/
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <pthread.h>
using namespace std;
struct Counter {
int value;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
Counter(int value) : value(value) { }
};
struct ProcessFilesArg {
const vector<string> * lstFileName;
Counter * counter;
};
void checkAuthUser() {
cout << "[ Auth ] Start" << endl;
// Send request to authenticator, check permissions, encrypt, decrypt...
sleep(20);
cout << "[ Auth ] Done" << endl;
}
void* processFiles(void* argVoid) {
auto arg = (ProcessFilesArg*) argVoid;
auto&& lstFileName = *arg->lstFileName;
auto&& counter = *arg->counter;
for (auto&& fileName : lstFileName) {
// Read file
cout << "[ ReadFile ] Start " << fileName << endl;
sleep(10);
cout << "[ ReadFile ] Done " << fileName << endl;
pthread_mutex_lock(&counter.mut);
{
--counter.value;
pthread_cond_signal(&counter.cond);
}
pthread_mutex_unlock(&counter.mut);
// Write log into disk
sleep(5);
cout << "[ WriteLog ]" << endl;
}
return nullptr;
pthread_exit(nullptr);
}
void processRequest() {
const vector<string> lstFileName = { "foo.html", "bar.json" };
Counter counter(lstFileName.size());
pthread_t tid;
ProcessFilesArg arg = { &lstFileName, &counter };
// The server checks auth user while reading files, concurrently
pthread_create(&tid, nullptr, &processFiles, &arg);
checkAuthUser();
// The server waits for completion of loading files
pthread_mutex_lock(&counter.mut);
{
while (counter.value > 0) {
pthread_cond_wait(&counter.cond, &counter.mut);
}
}
pthread_mutex_unlock(&counter.mut);
cout << "\nNow user is authorized and files are loaded" << endl;
cout << "Do other tasks...\n" << endl;
pthread_join(tid, nullptr);
pthread_cond_destroy(&counter.cond);
pthread_mutex_destroy(&counter.mut);
}
int main() {
processRequest();
return 0;
}