MCPcopy Create free account
hub / github.com/Codesire-Deng/co_context / Guide

Class Guide

example/sem_std.cpp:28–76  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26}
27
28class alignas(128 /*std::hardware_destructive_interference_size*/) Guide {
29 inline static std::mutex cout_mutex;
30 inline static std::chrono::time_point<std::chrono::high_resolution_clock>
31 started_at;
32 unsigned delay{rnd()}, occupy{rnd()}, wait_on_sema{};
33
34 public:
35 static void start_time() {
36 started_at = std::chrono::high_resolution_clock::now();
37 }
38
39 void initial_delay() { std::this_thread::sleep_for(delay * time_tick); }
40
41 void occupy_sema() {
42 wait_on_sema = static_cast<unsigned>(
43 std::chrono::duration_cast<std::chrono::milliseconds>(
44 std::chrono::high_resolution_clock::now() - started_at
45 - delay * time_tick
46 )
47 .count()
48 / time_tick.count()
49 );
50 std::this_thread::sleep_for(occupy * time_tick);
51 }
52
53 void visualize(unsigned id, unsigned x_scale = 2) const {
54 auto cout_n = [=](auto str, unsigned n) {
55 n *= x_scale;
56 while (n-- > 0) {
57 std::cout << str;
58 }
59 };
60 auto lk = std::lock_guard(cout_mutex);
61
62 std::cout << "#" << std::setw(2) << id << " ";
63 cout_n("░", delay);
64 cout_n("▒", wait_on_sema);
65 cout_n("█", occupy);
66 std::cout << '\n';
67 }
68
69 static void show_info() {
70 std::cout
71 << "\nThreads: " << max_threads
72 << ", Throughput: " << max_sema_threads
73 << " │ Legend: initial delay ░░ │ wait state ▒▒ │ sema occupation ██ \n"
74 << std::endl;
75 }
76};
77
78std::array<Guide, max_threads> guides;
79

Callers

nothing calls this directly

Calls 1

rndFunction · 0.70

Tested by

no test coverage detected