-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexam.cc
More file actions
76 lines (62 loc) · 1.86 KB
/
exam.cc
File metadata and controls
76 lines (62 loc) · 1.86 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
# include <cassert>
# include <iostream>
# include <functional>
# include <vector>
# include <queue>
# include <thread>
# include <condition_variable>
# include <set>
# include <utils.H>
# include <item_queue.H>
# include <outputfile.H>
# include <sorting.H>
using namespace std;
void worker_thread(ItemQueue & q, OutputFile & output,
function<void(vector<long>&)> sort_method)
{
while (true)
{
optional<string> item = q.get();
if (not item.has_value())
break; // if no item ==> writing into queue has finished
vector<long> v = item_to_vector(move(item.value()));
sort_method(v);
output.write_vector(v);
}
}
constexpr size_t Max_Num_Of_Threads = 4;
int main(int argc, char * argv[])
{
raise_domain_error_unless(argc == 4)
<< "Usage is" << endl
<< " ./test path-input-file path-output-file quick|merge" << endl;
const string sort_algo = argv[3];
raise_domain_error_unless(sort_algo == "quick" or sort_algo == "merge")
<< "Last parameter must be \"quick\" or \"merge\"" << endl;
const string input_name = argv[1];
ifstream input(input_name);
raise_domain_error_unless(input.is_open()) << "cannot open " << input_name;
ItemQueue q;
OutputFile output(argv[2]);
function<void(vector<long>&)> sort_method;
if (sort_algo == "merge")
sort_method = merge_sort;
else
sort_method = quick_sort;
// Prepare the worker threads
vector<thread> workers;
for (size_t i = 0; i < Max_Num_Of_Threads; ++i)
{
thread th(worker_thread, ref(q), ref(output), sort_method);
workers.push_back(move(th));
}
// Now we read input file and put every line into the protected
// queue. Suspended threads will resume and consume them as they
// are inserted
string line;
while (getline(input, line))
q.put(move(line));
q.finish();
for (auto & th : workers)
th.join();
}