-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
80 lines (62 loc) · 1.74 KB
/
test.cpp
File metadata and controls
80 lines (62 loc) · 1.74 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
#include <cstdlib>
#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <vector>
#include <signal.h>
bool exiting = false;
void handler(int signum) {
exiting = true;
}
using namespace std::chrono;
class Timer {
private:
static double startTime;
public:
static void init(double start = -1) {
startTime = start == -1 ? now() : start;
}
static double now() {
return 0.001 * 0.001 * 0.001 * system_clock::now().time_since_epoch().count();
}
/**
* Returns elapsed time since program start (since MyMpi::init) in seconds.
*/
static float elapsedSeconds() {
return now() - startTime;
}
};
double Timer::startTime;
int main(int argc, const char** argv) {
using namespace std::chrono_literals;
Timer::init();
signal(SIGINT, handler);
signal(SIGTERM, handler);
size_t numThreads = 1;
if (argc > 1) {
numThreads = atoi(argv[1]);
}
float timelim = -1;
if (argc > 2) {
timelim = atof(argv[2]);
}
std::cout << numThreads << " threads, time limit: " << timelim << "s" << std::endl;
auto execute = [&, timelim]() {
std::vector<int> numbers;
while (!exiting && (timelim < 0 || Timer::elapsedSeconds() < timelim)) {
int num = 0;
for (int i = numbers.size()-10; i < (int)numbers.size(); i++) {
if (i >= 0) num += numbers[i];
}
numbers.push_back(num);
}
};
std::vector<std::thread> threads;
for (size_t i = 1; i < numThreads; i++) {
threads.emplace_back(execute);
}
execute();
for (auto& thread : threads) thread.join();
std::cout << "Program exiting." << std::endl;
}