1 | #include <vector> |
---|---|
2 | #include <thread> |
3 | #include <iostream> |
4 | #include <Common/Stopwatch.h> |
5 | |
6 | |
7 | int main(int, char **) |
8 | { |
9 | static constexpr size_t num_threads = 10; |
10 | static constexpr size_t num_iterations = 3; |
11 | |
12 | std::vector<std::thread> threads(num_threads); |
13 | |
14 | AtomicStopwatch watch; |
15 | Stopwatch total_watch; |
16 | |
17 | for (size_t i = 0; i < num_threads; ++i) |
18 | { |
19 | threads[i] = std::thread([i, &watch, &total_watch] |
20 | { |
21 | size_t iteration = 0; |
22 | while (iteration < num_iterations) |
23 | { |
24 | if (auto lock = watch.compareAndRestartDeferred(1)) |
25 | { |
26 | std::cerr << "Thread "<< i << ": begin iteration "<< iteration << ", elapsed: "<< total_watch.elapsedMilliseconds() << " ms.\n"; |
27 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
28 | std::cerr << "Thread "<< i << ": end iteration "<< iteration << ", elapsed: "<< total_watch.elapsedMilliseconds() << " ms.\n"; |
29 | ++iteration; |
30 | } |
31 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
32 | } |
33 | }); |
34 | } |
35 | |
36 | for (auto & thread : threads) |
37 | thread.join(); |
38 | |
39 | return 0; |
40 | } |
41 |