forked from feiying123/training-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummary.cpp
More file actions
58 lines (53 loc) · 1.76 KB
/
Copy pathsummary.cpp
File metadata and controls
58 lines (53 loc) · 1.76 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
#include "test.h"
#include <atomic>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <sstream>
#include <thread>
#include <vector>
constexpr auto MAX_EXERCISE = 33;
int main(int argc, char **argv) {
if (argc == 1) {
Log log{Console{}};
for (auto i = 0; i <= MAX_EXERCISE; ++i) {
log << i;
}
std::cout << std::accumulate(log.result.begin(), log.result.end(), 0, std::plus{}) << '/' << MAX_EXERCISE + 1 << " [";
for (auto b : log.result) {
std::cout << (b ? "\x1b[32m#\x1b[0m" : "\x1b[31mX\x1b[0m");
}
std::cout << ']' << std::endl;
return EXIT_SUCCESS;
}
if (argc == 2 && std::strcmp(argv[1], "--simple") == 0) {
auto concurrency = std::thread::hardware_concurrency();
if (concurrency == 0) {
concurrency = 1;
}
std::atomic_int k{0};
std::vector<std::thread> threads;
threads.reserve(concurrency);
std::cout << "concurrency: " << concurrency << std::endl;
Log log{Null{}};
for (auto i = 0u; i <= concurrency; ++i) {
threads.emplace_back([i, &log, &k] {
int j = k.fetch_add(1);
while (j <= MAX_EXERCISE) {
std::printf("run %d at %d\n", j, i);
log << j;
j = k.fetch_add(1);
}
});
}
for (auto &thread : threads) {
thread.join();
}
std::cout << std::accumulate(log.result.begin(), log.result.end(), 0, std::plus{}) << '/' << MAX_EXERCISE + 1 << std::endl;
return EXIT_SUCCESS;
}
std::cerr << "Usage: xmake run summary [--simple]" << std::endl;
return EXIT_FAILURE;
}