Skip to content

Commit 5bf13ce

Browse files
authored
Refactor thread_proc and remove unused function
1 parent 5726a19 commit 5bf13ce

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

Code Samples/Fib/thread.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@
77
#include <mutex>
88

99
static std::atomic<int> g_tid{0};
10-
1110
static std::mutex cout_mutex;
1211

1312
static 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-
2417
static thread_local std::mt19937_64 rng{std::random_device{}()};
2518

2619
static int intRand(int min, int max) {
@@ -30,22 +23,20 @@ static int intRand(int min, int max) {
3023
void 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+
}

0 commit comments

Comments
 (0)