Skip to content

Commit 93a3f97

Browse files
authored
Update main.cpp
1 parent fa931f6 commit 93a3f97

File tree

1 file changed

+18
-35
lines changed

1 file changed

+18
-35
lines changed

Code Samples/Fib/main.cpp

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
#include <chrono>
99
#include <csignal>
1010
#include <unistd.h>
11-
1211
#include "thread.h"
1312

1413
#define THREAD_COUNT 10
1514

1615
static constexpr char block[] = "--block";
1716
static constexpr char crash[] = "--crash";
1817
static constexpr char test_flag[] = "--test";
19-
std::atomic<int> test_count{0}; // Thread-safe counter
18+
2019
volatile std::sig_atomic_t g_signal_status = 0;
2120

2221
void signal_handler(int signal) {
@@ -26,61 +25,45 @@ void signal_handler(int signal) {
2625
int main(int argc, char **argv)
2726
{
2827
std::signal(SIGINT, signal_handler);
29-
30-
std::srand(static_cast<unsigned>(std::time(nullptr)));
31-
3228
std::cout << "Hello World!" << std::endl;
33-
29+
3430
if (argc == 2) {
3531
if (std::strcmp(block, argv[1]) == 0) {
3632
std::cout << "Attach a debugger and set foo=0 to continue" << std::endl;
3733
std::cout << "Process ID: " << getpid() << std::endl;
38-
39-
volatile int foo = 1;
40-
while (foo && g_signal_status == 0) {
34+
std::atomic<int> foo{1}; // Changed from volatile
35+
while (foo.load() && g_signal_status == 0) {
4136
std::this_thread::sleep_for(std::chrono::seconds(1));
4237
std::cout << "Waiting... (press Ctrl-C to quit)" << std::endl;
4338
}
4439
return 0;
4540
}
4641
else if (std::strcmp(crash, argv[1]) == 0) {
4742
std::cout << "Triggering intentional crash..." << std::endl;
48-
volatile int foo = 0;
49-
volatile int bar = 1 / foo;
43+
std::atomic<int> foo{0}; // Changed from volatile
44+
std::atomic<int> bar{1 / foo.load()}; // Changed from volatile and added .load()
5045
(void)bar;
5146
return 1;
5247
}
5348
else if (std::strcmp(test_flag, argv[1]) == 0) {
5449
std::cout << "Running in test mode" << std::endl;
5550
}
5651
}
57-
52+
5853
std::vector<std::thread> threads;
5954
threads.reserve(THREAD_COUNT);
60-
61-
try {
62-
for (int i = 0; i < THREAD_COUNT; ++i) {
63-
std::cout << "Launching thread " << i << std::endl;
64-
threads.emplace_back(thread_proc);
65-
}
66-
67-
for (auto& t : threads) {
68-
if (t.joinable()) {
69-
t.join();
70-
test_count.fetch_add(1, std::memory_order_relaxed);
71-
}
72-
}
55+
56+
for (int i = 0; i < THREAD_COUNT; ++i) {
57+
std::cout << "Launching thread " << i << std::endl;
58+
threads.emplace_back(thread_proc);
7359
}
74-
catch (const std::exception& e) {
75-
std::cerr << "Error: " << e.what() << std::endl;
76-
for (auto& t : threads) {
77-
if (t.joinable()) {
78-
t.detach();
79-
}
60+
61+
for (auto& t : threads) {
62+
if (t.joinable()) {
63+
t.join();
8064
}
81-
return 1;
8265
}
83-
84-
std::cout << "\nAll " << test_count.load() << " threads completed successfully!" << std::endl;
66+
67+
std::cout << "\nAll " << threads.size() << " threads completed successfully!" << std::endl;
8568
return 0;
86-
}
69+
}

0 commit comments

Comments
 (0)