-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathexer07c-data-server.cpp
More file actions
70 lines (47 loc) · 1.5 KB
/
Copy pathexer07c-data-server.cpp
File metadata and controls
70 lines (47 loc) · 1.5 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
/*
THE DATA SERVER PROBLEM
Version C: Solving the problem using a count-down latch
*/
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include <thread>
#include <latch>
using namespace std;
#define sleepsec(secs) \
do { std::this_thread::sleep_for(std::chrono::seconds(secs)); } while (0)
void checkAuthUser() {
cout << "[ Auth ] Start" << endl;
// Send request to authenticator, check permissions, encrypt, decrypt...
sleepsec(20);
cout << "[ Auth ] Done" << endl;
}
void processFiles(const vector<string>& lstFileName, std::latch& rdLatch) {
for (auto&& fileName : lstFileName) {
// Read file
cout << "[ ReadFile ] Start " << fileName << endl;
sleepsec(10);
cout << "[ ReadFile ] Done " << fileName << endl;
rdLatch.count_down();
// Write log into disk
sleepsec(5);
cout << "[ WriteLog ]" << endl;
}
}
void processRequest() {
const vector<string> lstFileName = { "foo.html", "bar.json" };
std::latch readFileLatch(lstFileName.size());
// The server checks auth user while reading files, concurrently
std::thread th(&processFiles, std::cref(lstFileName), std::ref(readFileLatch));
checkAuthUser();
// The server waits for completion of loading files
readFileLatch.wait();
cout << "\nNow user is authorized and files are loaded" << endl;
cout << "Do other tasks...\n" << endl;
th.join();
}
int main() {
processRequest();
return 0;
}