-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
222 lines (185 loc) · 7.12 KB
/
main.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "judger.h"
#include <chrono> // for time measurement
#include <condition_variable> // for condition_variable
#include <fstream> // for read request file
#include <functional> // for std::ref
#include <iostream> // for std::cout, std::cerr
#include <mutex> // for std::mutex
#include <queue> // for std::queue
#include <string> // for std::string
#include <thread> // for multithreading
#include <vector> // for std::vector
using namespace std;
std::string PARTICIPANT_CODE = "Submit/probA_AC.cpp";
std::string INPUT_DIR = "problem/probA/testcases/";
std::string OUTPUT_DIR = "problem/probA/expected_outputs/";
// The print function represents a task that takes a string reference as input
// and prints it.
void print(string &s) {
cout << "=====" << s << " is running on thread " << this_thread::get_id()
<< endl;
// Simulate some work
this_thread::sleep_for(chrono::milliseconds(100));
}
class ThreadPool {
private:
// A list of threads in the pool
vector<thread> threads;
vector<bool> idle_thread;
// A queue of tasks
queue<function<void()>> tasks;
// Mutex and condition variable for synchronization
mutex queue_mutex;
condition_variable condition;
// A flag to stop the pool
bool stop;
public:
/*
Constructor:
- init stop flag to False, meaning the pool is running
- create a number of threads and add them to the pool, each thread
have a while loop, that will keep the thread running until the pool
is stopped. The thread will wait for a task to be added to the queue
and then execute the task.
*/
ThreadPool(int num_threads) : stop(false) {
// set up idle_thread
for (int i = 0; i < num_threads; ++i)
idle_thread.push_back(true);
// For each thread, create a lambda function that will keep the
// thread running
for (int i = 0; i < num_threads; i++) {
// Create a thread, add to the list of threads
threads.emplace_back([this, i] {
while (true) {
// Create a task variable, which has no task initially
function<void()> task;
{
// Lock the mutex in this scope, to protect the tasks
// queue
unique_lock<mutex> lock(this->queue_mutex);
// Wait for a task to be added to the queue, or the pool
// to be stopped
this->condition.wait(lock, [this] {
return this->stop || !this->tasks.empty();
});
// If the pool is stopped and the queue is empty, then
// return
if (this->stop && this->tasks.empty())
return;
// Get the nearest task from the queue
task = move(this->tasks.front());
// thread gets task => idle = false
this->idle_thread[i] = false;
// Remove the task from the queue
this->tasks.pop();
}
// Execute the task
task();
// finish task => idle = true
this->idle_thread[i] = true;
}
});
}
}
/*
* add_task function: add a task (function) to the task queue
*/
void add_task(function<void()> task) {
{
// Lock the mutex to protect the tasks queue
unique_lock<mutex> lock(queue_mutex);
// If the pool is stopped, throw an exception
if (stop)
throw runtime_error("ThreadPool is stopped");
// Add the task to the queue
tasks.push(move(task));
}
// Notify one of the threads to execute the task
condition.notify_one();
}
// check if finishing all task
bool finish_all_tasks() {
for (bool idle_i : idle_thread) {
if (idle_i == false)
return false;
}
if (tasks.empty())
return true;
return false;
}
/*
Destructor: wait for all threads to finish executing their tasks
then join the threads and exit the program
*/
~ThreadPool() {
{
// Lock the mutex to protect the tasks queue
unique_lock<mutex> lock(queue_mutex);
// Set the stop flag to true, meaning the pool is stopped
stop = true;
}
// Notify all threads to stop
condition.notify_all();
// Join all threads
for (thread &t : threads) {
t.join();
}
}
};
int main(int argc, char *argv[]) {
// get start time
auto start_time = std::chrono::high_resolution_clock::now();
// Usage: ./main <numOfString> <NumOfThreads>
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <request>.txt\n";
return 1;
}
ifstream request_file;
request_file.open("Test/" + string(argv[1]) + ".txt", ios::in);
int num_threads, num_tasks;
request_file >> num_tasks >> num_threads;
// Create a thread pool with num_threads threads
ThreadPool pool(num_threads);
for (int i = 0; i < num_tasks; i++) {
// sleep until new submit
int time_arrive;
request_file >> time_arrive;
auto current_time = std::chrono::high_resolution_clock::now();
int time_to_next_submit =
time_arrive - chrono::duration_cast<chrono::milliseconds>(
current_time - start_time)
.count();
if (time_to_next_submit > 0)
this_thread::sleep_for(chrono::milliseconds(time_to_next_submit));
// problem of submit
string problem;
request_file >> problem;
problem = "../problem/" + problem;
INPUT_DIR = "problem/" + problem + "/testcases/";
OUTPUT_DIR = "problem/" + problem + "/expected_outputs/";
// code directory of submit
string dir_code;
request_file >> dir_code;
PARTICIPANT_CODE = "Submit/" + dir_code + ".cpp";
dir_code = "Submit/" + dir_code + ".cpp";
cout << "Adding task " << problem << " for code " << dir_code
<< " to the pool at time " << time_arrive << endl;
string exit_code, message;
pool.add_task(bind(judge, i, dir_code, INPUT_DIR, OUTPUT_DIR));
}
// calculate total time to process all tasks
while (true) {
if (pool.finish_all_tasks()) {
auto end_time = std::chrono::high_resolution_clock::now();
int total_time = chrono::duration_cast<chrono::milliseconds>(
end_time - start_time)
.count();
cout << "the OJ system takes " << total_time
<< " milliseconds to finish\n";
break;
}
this_thread::sleep_for(chrono::milliseconds(100));
}
return 0;
}