File tree Expand file tree Collapse file tree 1 file changed +4
-13
lines changed Expand file tree Collapse file tree 1 file changed +4
-13
lines changed Original file line number Diff line number Diff line change 77#include < mutex>
88
99static std::atomic<int > g_tid{0 };
10-
1110static std::mutex cout_mutex;
1211
1312static int fib (int n) {
1413 if (n <= 1 ) return 1 ;
1514 return fib (n - 1 ) + fib (n - 2 );
1615}
1716
18- static void set_thread_name (const std::string& name) {
19- #ifdef __cpp_lib_jthread
20- #else
21- #endif
22- }
23-
2417static thread_local std::mt19937_64 rng{std::random_device{}()};
2518
2619static int intRand (int min, int max) {
@@ -30,22 +23,20 @@ static int intRand(int min, int max) {
3023void thread_proc () {
3124 const int tid = g_tid.fetch_add (1 , std::memory_order_relaxed);
3225 const std::string thread_name = " Thread " + std::to_string (tid);
33- set_thread_name (thread_name);
34-
26+
3527 const auto delay = std::chrono::nanoseconds (500000000 + intRand (0 , 500000000 ));
36-
3728 std::this_thread::sleep_for (delay);
38-
29+
3930 for (int i = 0 ; i <= 30 ; ++i) {
4031 {
4132 std::lock_guard<std::mutex> lock (cout_mutex);
4233 std::cout << thread_name << " : fib(" << i << " ) = " << fib (i) << std::endl;
4334 }
4435 std::this_thread::sleep_for (delay);
4536 }
46-
37+
4738 {
4839 std::lock_guard<std::mutex> lock (cout_mutex);
4940 std::cout << thread_name << " exited!" << std::endl;
5041 }
51- }
42+ }
You can’t perform that action at this time.
0 commit comments